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




"Charlie Gordon" <news@xxxxxxxxxxx> wrote in message
news:df2rq7$h3e$1@xxxxxxxxxxxxxxxxxxxxxx
> "Keith Thompson" <kst-u@xxxxxxx> wrote in message
> news:lnu0h7qh5r.fsf@xxxxxxxxxxxxxxxxxx
> > Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx> writes:
> > > pete <pfiland@xxxxxxxxxxxxxx> writes:
> >
> > I won't try to speak for pete, but since "while (1)" and "for (;;)"
> > are semantically identical, I'd be very surprised if there were any
> > difference in defect rate. It's something that seems so obvious to me
> > that I wouldn't bother trying to measure it without a very good
> > reason. If there were a difference, I'd tend to assume that it's a
> > difference in training (perhaps the books or classes that use one form
> > happen, by coincidence to be better than the ones that use the other
> > form). Do you have some reason to think there's a significant
> > difference?
>
> Assume the programmer always uses while(1) { ... } for his endless loops.
> You quickly get used to interpreting these automatically as you read them,
> without paying much attention.
> What if there are some while(l) { ... } loops as well. Will you always
keep a
> sharp enough eye to catch these correctly ?
>
> I prefer for(;;). That way anything that looks like while(l) will catch
my eye,
> and I will no doubt complain about such a poor choice for a variable name.
>
> Therefore, I agree while(1) is more prone to indirectly causing defect
than
> for(;;).
>

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


You can further document it like this:
while (!done) { // will never be done while computer is running
...

There's many variations on this:

#define NOTDONE 1
...
while (NOTDONE) {
...

Convey your intent in English (assuming English is your first language)

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




.



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 ( ;; )
    ... > difference in defect rate. ... Do you have some reason to think there's a significant ... sharp enough eye to catch these correctly? ... That way anything that looks like whilewill catch my eye, ...
    (comp.lang.c)