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



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



Relevant Pages

  • Re: The origins of asd advice and information
    ... What one regards as closed or open loop control depends on what one ... you make the tests and adjustments, which if you want can be less than ... Yet that's exactly how many diabetics "manage" their blood sugar ...
    (alt.support.diabetes)
  • Re: Poor style loops?
    ... >> loop control variables. ... > tests in the loop control statement that aren't part of the loop control ... a reason that is an exception from "normal" flow) rarely ...
    (alt.comp.lang.learn.c-cpp)
  • Re: how do people feel about exit function from loop
    ... for the purpose of controlling a loop, ... always makes perfect sense to me as a loop control. ... Isn't an exit for that much simpler tho? ...
    (microsoft.public.vb.general.discussion)
  • Re: Poor style loops?
    ... I think for-loops are most useful because you can usually ... see the whole logic underlying the loop in that one place. ... In almost any for-loop I write, there is a single loop control ... If that really is the intent, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: how do people feel about exit function from loop
    ... even in means checking a flag just for that purpose. ... and it is useful following the loop for deciding what to do next. ... always makes perfect sense to me as a loop control. ...
    (microsoft.public.vb.general.discussion)