Re: while (1) vs. for ( ;; )
- From: Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx>
- Date: 30 Aug 2005 12:38:42 -0700
Richard Heathfield <invalid@xxxxxxxxxxxxxxx> writes:
> Michael B Allen said:
>
> > Should there be any preference between the following logically equivalent
> > statements?
> >
> > while (1) {
> >
> > vs.
> >
> > for ( ;; ) {
> >
> > I suspect the answer is "no" but I'd like to know what the consensus is
> > so that it doesn't blink through my mind anymore when I type it.
>
> [snip]
>
> Personally, I prefer neither choice! I would rather have the loop control
> statement explicitly document the exit condition (unless there genuinely
> isn't one, such as might be the case in an electronic appliance like a
> microwave oven, where "forever" can roughly be translated as "whilst power
> is being supplied to the appliance").
What Richard might be saying, but isn't really what I think
he's trying to say, is that the control expression should
redundantly express the condition for loop exit, even if the
loop is never exited by the test on the loop control. So
for example,
/* p != NULL; */
while( p != NULL ){
...
...
p = blah_blah_blah();
if( p == NULL ) break;
...
... code that doesn't affect p ...
}
If this is what he's saying it's an interesting idea. Just
offhand I don't remember seeing it before.
My first reaction was that he's meaning to say that loops
that look infinite (but aren't) are bad, and the code should
be reworked so that the loop control expression is really
what controls the loop body. So to rework the example
above, it might come out as
... /* these lines were at the start of the */
... /* "infinite" loop body originally */
while( (p = blah_blah_blah()) != NULL ){
...
... code that doesn't affect p ...
... /* these lines were at the start of the */
... /* "infinite" loop body originally */
}
which is often the right idea. Certainly in code review any
loop that is of the "infinite-but-not-really" variety should
get some scrutiny. In many cases the code will benefit from
being reworked so that the loop control expression is what
causes the loop to exit (or at least one of them).
However, I don't believe that all "infinite-but-not-really"
loops benefit from this kind of rewriting. Even for loops
with only one exit condition, sometimes having the only loop
exit be a break (or return) in the middle of the loop body
is the clearest expression of what the loop is supposed to
do. At least, that has been my experience.
.
- Follow-Ups:
- Re: while (1) vs. for ( ;; )
- From: Christian Bau
- Re: while (1) vs. for ( ;; )
- From: Alan Balmer
- Re: while (1) vs. for ( ;; )
- From: Richard Heathfield
- Re: while (1) vs. for ( ;; )
- References:
- while (1) vs. for ( ;; )
- From: Michael B Allen
- Re: while (1) vs. for ( ;; )
- From: Richard Heathfield
- while (1) vs. for ( ;; )
- Prev by Date: Re: Why C/C++ errors are SO obscure/devious??
- Next by Date: Re: Problem in Strdup()
- Previous by thread: Re: while (1) vs. for ( ;; )
- Next by thread: Re: while (1) vs. for ( ;; )
- Index(es):
Relevant Pages
|