No loop condition in for and while loop

The standard requires that the omitted condition for for loop is replaced by a non-zero constant:

From C11 6.8.5.3: (emphasis mine)

6.8.5.3 The for statement

1 The statement for ( clause-1 ; expression-2 ; expression-3 )
statement behaves as follows: The expression expression-2 is the
controlling expression that is evaluated before each execution of the
loop body. The expression expression-3 is evaluated as a void
expression after each execution of the loop body. If clause-1 is a
declaration, the scope of any variables it declares is the remainder
of the declaration and the entire loop, including the other two
expressions; it is reached in the order of execution before the first
evaluation of the controlling expression. If clause-1 is an
expression, it is evaluated as a void expression before the first
evaluation of the controlling expression.134)

2 Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

Since there’s no such requirement for while loop (if the condition is omitted), I believe, it’s left to the implementation of compiler.

Leave a Comment