Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No loop condition in for and while loop

Tags:

c

loops

while(cond) // fine
for(;cond;) //fine

but when I remove the conditional part

while() //syntax compilation error 
for(;;) //Infinite loop

How these loops are internally implemented ? Or,how does compiler (parser) know that empty condition in while is error and in for as Infinite?

I didn't find anything about this particularly, I think guys like me (who are beginner) in C might have same confusion

like image 363
Omkant Avatar asked Oct 30 '12 20:10

Omkant


4 Answers

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.

like image 96
P.P Avatar answered Nov 10 '22 04:11

P.P


There is no technical reason explaining why one works and the other doesn't. It is a human factors consideration of the language designers. They felt that an infinite loop using for (;;) made more sense than while (). They may have been influenced by ALGOL, a language used by cavemen.

like image 40
wallyk Avatar answered Nov 10 '22 06:11

wallyk


...how does compiler (parser) know that empty condition in while is error and in for as Infinite?

Because the language definition specifies it, in both the syntax (grammar) and semantics.

Here's the syntax for a while loop:

while ( expression ) statement

and here's the syntax for a for loop (as of C2011):

for ( expressionopt ; expressionopt ; expressionopt ) statement
for ( declaration expressionopt ; expressionopt ) statement

The subscript opt in each expressionopt in the for statement indicates that the corresponding expression is optional. This is reinforced by the text:

6.8.5.3 The for statement

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

By contrast, the controlling expression of the while statement is not marked as optional, which is also reinforced in the text:

6.8.5.1 The while statement

1 The evaluation of the controlling expression takes place before each execution of the loop body.

Not much room there to interpret that the controlling expression may be omitted.

like image 3
John Bode Avatar answered Nov 10 '22 05:11

John Bode


The conditions determining syntax and semantic correctness of a program are encoded to the language grammar. Language grammars are made by language creators, they determine the language look-and-feel, like in case of C. I suppose the basic intuition behind for(;;) and while(1) is that any part of for(;;) may be omitted, though while(1) is totally sufficient for creating infinite loops, whereas while() would be a hacky corner case for just one narrow case.

like image 2
Dmytro Sirenko Avatar answered Nov 10 '22 05:11

Dmytro Sirenko