Re: while (1) vs. for ( ;; )



"Baxter" <lbax02.spamguard@xxxxxxxxxxx> writes:
[...]
> Why not code it like you would read it in English:
>
> bool done = false;
> while (!done) { // while not done do
> ...
>
> bool mainloop = true;
> while (mainloop) { // while mainloop do
> ...

Because clear English is not necessarily clear C.

You've declared "done" and "mainloop" as variables, which means their
values could change. With "while (1)" (or "for (;;)") I can see at a
glance that the loop is an infinite one; with your suggestion, I can
never be sure unless I analyze the program and verify that "done" will
always be false (and then I'll wonder why the heck you used a
variable).

The clear meaning of "while (!done)" is that the variable "done"
represents a condition that will become true when the loop is meant to
terminate.

I assume that anyone reading my code either is familiar with C, or
just won't be able to understand the code.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: while (1) vs. for ( ;; )
    ... >>> difference in defect rate. ... >Why not code it like you would read it in English: ... Balmer Consulting ...
    (comp.lang.c)
  • Re: while (1) vs. for ( ;; )
    ... >> Why not code it like you would read it in English: ... > glance that the loop is an infinite one; ... > represents a condition that will become true when the loop is meant to ... That program WILL terminate sometime. ...
    (comp.lang.c)
  • Re: Number of rows in xls file
    ... No problem with English. ... I think I can figure out how to stop the loop from the ... Gerry Metze ... >> data on you sheet? ...
    (microsoft.public.excel.misc)
  • Re: how do people feel about exit function from loop
    ... never mind that structured programming says you shouldn't ... Imagine a complex loop rendered into English: ... The For w Exits starts off boldly, ...
    (microsoft.public.vb.general.discussion)
  • Re: Plzz Explain !!
    ... The loop terminates when the middle bit evaluates to zero. ... stupid things like putting printfs in the condition. ... This is inherent inthe English language meaning of the words. ... That is not how I would talk in English about processing a file or a linked lists or many other things. ...
    (comp.lang.c)