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:
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 evaluatesfor
statements?Why was this behavior chosen for
for
loops lacking conditional expressions?