Re: while (1) vs. for ( ;; )
- From: Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx>
- Date: 30 Aug 2005 15:16:14 -0700
Keith Thompson <kst-u@xxxxxxx> writes:
> Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx> writes:
> > pete <pfiland@xxxxxxxxxxxxxx> writes:
> >
> >> Tim Rentsch wrote:
> >>
> >> > I was surprised at how many people reported that a compiler
> >> > they use issues a warning for 'while(1)' and gave that as a
> >> > reason for giving preference to the 'for(;;)' form. It
> >> > seems like a choice should be made on the basis of what's a
> >> > better expression (lower defect rate, higher productivity,
> >> > better understood by the readers), not on the basis of some
> >> > errant warning message.
> >>
> >> The warning is the tie breaker.
> >>
> >> There is no difference in defect rate,
> >
> > Is this statement just one of belief, or are you offering
> > some evidence?
>
> 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.
In fact, I believe the most likely situation is that there
aren't any differences in defect rate, at least if we're
talking about a measurable difference. I asked the question
because I think it's useful to get people to be explicit
about the assumptions that they're making, especially if the
assumptions are unconscious rather than conscious. The
causes of defects are like performance bottlenecks - where
you think they are is often not where they actually are, and
there's no substitute for actual measurement to find out.
> 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?
If there is a difference, I would guess that the factors
involved are correlative rather than causitive (like the
example you gave about training. I don't have any reason to
think that there's a significant difference; but not having
a reason to think there *is* a significant difference should
not, by itself, be enough to claim that there *isn't* any.
There should be some sort of evidence, even if it's just
anecdotal.
> >> productivity,
> >
> > There is for some developers. Among other things, some
> > debugging techniques work better when the 'while(1)' form is
> > used. If the people who prefer the 'for(;;)' form don't see
> > any difference in productivity, that seems to be an argument
> > in favor of using the 'while(1)' form.
>
> What debugging techniques are you referring to? Off the top of my
> head, I'd say that any debugging technique that treats the two forms
> significantly differently is broken, but I'm prepared to be
> enlightened.
One technique I was thinking of is, eg,
#define while(e) while( LOOPTRACK() && (e) )
which is a technique I've found useful in the past in some
situations. You can do something similar with 'for()', but
it doesn't work quite as well because of the syntax of the
'for' control expressions; in particular, it's hard to put
the call to 'LOOPTRACK()' before the first iteration. This
technique can be especially important for loops of the "near
infinite" variety.
> >> or understanding by the readers.
> >
> > At least one poster in this thread said something along
> > the lines of using 'while' for "looping" and 'for' for
> > "iteration". In the non-infinite case, that's usually
> > my leaning also. So there is *some* difference in how
> > the two forms are understood.
>
> I read both "while (1)" and "for (;;)" as "this is an infinite loop,
> or at least one for which the termination condition is not specified",
> and any C programmer should be familiar with both forms. I see the
> point of preferring "for (;;)" when iterating over some set of
> discrete entities, but I don't think it's a strong argument.
I'm not saying it should be read that way, only that some
people do tend to read it that way. Whether, or how well,
something is understood by the readers depends on who "the
readers" are. Most people (myself included) tend to assume
unconsciously that other people will read things they same
way that they do. That assumption is at least partly untrue
for the 'while(1)/for(;;)' distinction.
Does this make a big difference? Perhaps so, perhaps not.
However I think it's important to note that the distinction
is there, even if later we decide that it isn't important
enough to worry about.
> In another thread, I've railed against "if (0 == a)", even though it's
> semantically identical to "if (a == 0)". And yes, I'm taking the
> opposite side of this argument. The difference, which is largely in
> my head, is that I don't find either "while (1)" or "for (;;)" to be
> ugly or jarring.
My personal opinion is that either 'while(1)' or 'for(;;)'
is ok, and developers should be free to write either one.
For myself I find the 'while(1)' to be more natural, and I'm
sure there are other people who find the 'for(;;)' more
natural; probably it roughly balances out. However, that
conclusion is merely personal opinion not supported by any
specific evidence. An important point in most of what I've
been saying is that it helps to first state points of
evidence, and only later in a separate step draw conclusions
based on whatever evidence is being considered.
.
- References:
- while (1) vs. for ( ;; )
- From: Michael B Allen
- Re: while (1) vs. for ( ;; )
- From: Tim Rentsch
- Re: while (1) vs. for ( ;; )
- From: pete
- Re: while (1) vs. for ( ;; )
- From: Tim Rentsch
- Re: while (1) vs. for ( ;; )
- From: Keith Thompson
- while (1) vs. for ( ;; )
- Prev by Date: Re: Why C/C++ errors are SO obscure/devious??
- Next by Date: Re: Why C/C++ errors are SO obscure/devious??
- Previous by thread: Re: while (1) vs. for ( ;; )
- Next by thread: Re: while (1) vs. for ( ;; )
- Index(es):