6

The answers to a recent question about for(;;){} loops (What does a for (;;) loop do) did not seem to answer something for me, so I thought that I would try to refine the question a bit. In particular, beyond knowing that for loops without conditionals are infinite loops, I would like to know why they are infinite loops.

In the statement for (;_;){}, the _ is a conditional expression. My first guess would be that an empty expression might evaluate to 0 or NULL. But if you test:

for (;;){}

is an infinite loop, as everyone has pointed out.

for (;1;){}

is an infinite loop.

But neither of these loop bodies execute at all:

for (;0;){}
for (;NULL;){}

Thus, the empty conditional expression does not seem to evaluate to either 0 or NULL.

So, my question: is the behavior of the for (;;){} loop an artifact of the way that C evaluates expressions, or is it just a special implementation-defined case, because a loop body that never executes is not very useful?

UPDATE: After reading the comments and answers, I realize that my question wasn't as clearly formulated as it might have been. I suppose that the question was two-fold:

  1. Is the behavior of for(;;){} loops strictly a result of the way that C evaluates expressions in general, or is this behavior specific to the way that C evaluates for statements?

  2. Why was this behavior chosen for for loops lacking conditional expressions?

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • 2
    Can you think of a more-descriptive question title? Ideally, the title should help others with the same question arrive here in the future. – Matt Ball Oct 01 '16 at 17:08
  • "My first guess would be that an empty expression might evaluate to `0` or `NULL`" - why? Neither C nor C++ have such concept as "empty expression". Which immediately means that empty space in place of `for` condition reqiures special treatment, which it is given in the specification of `for`'s behavior. – AnT stands with Russia Oct 01 '16 at 17:17
  • 1
    "Unclear what you're asking" LOL! – Lightness Races in Orbit Oct 01 '16 at 17:25
  • I just wondered if there was a default value for expected expressions in C. @tobi made a good point that you can't write, e.g. `while();`, or the compiler gives "error: expected expression ..." . So maybe I should have known that this would be specific to 'for()'. Just trying to dig a little deeper. – ad absurdum Oct 01 '16 at 17:30

2 Answers2

19

Both C and C++ guarantee this behaviour.


[C99: 6.8.5.3/1]: Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.


[C++14: 6.5.3/1]: The for statement

for ( for-init-statement conditionopt; expressionopt) statement

is equivalent to

{
   for-init-statement
   while ( condition ) {
      statement
      expression ;
   }
}

[..]

[C++14: 6.5.3/2]: Either or both of the condition and the expression can be omitted. A missing condition makes the implied while clause equivalent to while(true).

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

The conditional expression checks if the loop is to be continued, yes, and the default function of a loop is.. to loop. If it wasn't a special case having an empty test expression and continue the loop in this case, it would be consequently the opposite case (as you already noticed) and abort the loop which makes the entire for statement less powerful or even redundant.

The technique used here is called reasonable/sensible defaults.

Martin Sugioarto
  • 340
  • 3
  • 15
  • Thanks for addressing this part of the question. Please see my Update to the original question. – ad absurdum Oct 01 '16 at 20:03
  • Yes. It took me a bit of time to understand what you see as special there (and that your observation is correct) and I already knew at that time that I'll get downvoted, because not everyone takes this time. ;) – Martin Sugioarto Oct 01 '16 at 22:14