Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do for(;;) loops behave like infinite loops?

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?

like image 245
ad absurdum Avatar asked Oct 01 '16 17:10

ad absurdum


People also ask

Why is my for loop running infinitely?

An infinite loop occurs when a condition always evaluates to true. Usually, this is an error. For example, you might have a loop that decrements until it reaches 0.

Is a for loop an infinite loop?

One of the most common infinite loops is when the condition of the while statement is set to true. Below is an example of code that will run forever. Another classic example will be of the for loop where the terminating condition is set to infinity.

What causes an infinite loop in a while loop?

Basically, the infinite loop happens when the condition in the while loop always evaluates to true.

In which case a loop will become infinite?

Intended vs unintended looping Looping is repeating a set of instructions until a specific condition is met. An infinite loop occurs when the condition will never be met, due to some inherent characteristic of the loop.


1 Answers

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

like image 105
Lightness Races in Orbit Avatar answered Oct 19 '22 22:10

Lightness Races in Orbit