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



"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:ln4q96r875.fsf@xxxxxxxxxxxxxxxxxx
> "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.

It's not your job to write to the machine or to the compiler. Your job is
to write to the next guy (probably yourself) who will be reading the code.

>
> You've declared "done" and "mainloop" as variables, which means their
> values could change.

Yep.

>With "while (1)" (or "for (;;)") I can see at a
> glance that the loop is an infinite one;

Infinity is a very long time. Your program will absolutely stop before
then.

>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).

You could also use a #define -- while (PROGRAM_IS_RUNNING) {

>
> 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.

That program WILL terminate sometime. If nothing else, when the power
switch is flipped.

>
> I assume that anyone reading my code either is familiar with C, or
> just won't be able to understand the code.
>
Not a good assumption. Even you can mis-read your own code on a bad day -
you'll wonder the next day how you could have written such code.


--
---------------------------------------------------------------------
DataGet & PocketLog www.dataget.com
Data Collectors www.baxcode.com
--------------------------------------------------------------------





.



Relevant Pages