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



On Wed, 31 Aug 2005 21:12:25 -0700, "Baxter"
<lbax02.spamguard@xxxxxxxxxxx> wrote:

>"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.
>
Do you know any C programmer who would have difficulty reading this
code? Would you?
>>
>> 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.

You are unfamiliar with the term "infinite loop"?
>
>>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) {
>
Why? Since we are indeed writing a computer program, not a novel, I
still have to go find the definition of 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.

Are you saying that the power switch will change the value of "done"?
I suspect that's implementation-dependent.
>
>>
>> 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.

Much more likely to happen if you obfuscate the code in the ways
you're suggesting.
--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@xxxxxxx
.



Relevant Pages

  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)
  • Re: Problem with a script
    ... Okay, so variables have unique labels, that doesn't mean they still couldn't be handled in a loop. ... You still have them as uniquely named array indexes... ... I believe that for the new guy this code would be readable, and identifying problems should really not be any more difficult with this, plus I think that it actually might save some time to write the actual code from the beginnig, even though it's not at it's final stage, instead of first writing everything spread out, and then rewriting the same code again cleaned. ... If you expect a person to spend an hour reading your entire code and parsing it in their head, you wont get any help and have to solve the problem by yourself. ...
    (comp.lang.php)
  • Re: advice and/or help on glReadPixels
    ... I could not see any "BRGA" in the documentation. ... Is it possible that some cards are not ... >>> a) Try reading the entire RGB instead of just the red. ... a quick integer loop with a mask & shift is ...
    (comp.graphics.api.opengl)
  • Infinite loop in PSOS_INIT()
    ... ARM instruction set simulator) the processor goes into an infinite ... loop at PSOS_INIT. ... We are using ARMulator to simulate a system that has an ARM946ES ...
    (comp.arch.embedded)
  • Re: How to find out if process is in a loop?
    ... the problem of finding whether a problem is in an infinite ... For more information refer ... the program which i suspect to be in an infinite loop. ... the same stack repeatedly something is a problem. ...
    (comp.unix.aix)