Why should the condition for the loop remain empty?

Possible duplicate:
Missing loop condition for while and while loop

Why is a for-loop condition allowed to be left blank? for instance

 for (;;) 

compiles fine. Why is this empty expression evaluated to true , but the following

 if () {} while () {} 

will both fail? I want to know if / why for-loop is an exception in this case.

+4
source share
2 answers

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

These are iterative statements.

while (expression)

make a while statement (expression);

for (expression [opt]; expression [opt]; expression [opt])

for (expression expression [opt]; expression [opt])

The while loop was designed to evaluate the control expression before for each run of the loop, and the do loop was designed to evaluate after each run.

The for loop was developed as a more complex iteration statement.

6.8.5.3 for statement

Statement

for expression (expression-1; expression-2; expression-3)

behaves as follows: expression expression-2 is a control 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 sentence-1 is an announcement, the scope of any identifiers that it declares are the rest of the declaration and the full cycle, including two other expressions; this is achieved in order of execution prior to the first evaluation of the control expression. If sentence-1 is an expression, it is evaluated as the void expression before the first evaluation of the controlling expression.

Both sentences-1 and expression-3 may be omitted. The missing expression-2 is replaced by a nonzero constant.

The specification eliminates expression-2, a loop condition that is replaced by a nonzero constant. This means that the for loop will continue to run indefinitely.

This is useful for simple syntax for iteration without end.

 for(int i = 0;;i++) { //do stuff with i } 

It is much easier to write and understand than to write a while (1) loop with a variable declared outside the loop, and then grow inside the loop.

Then the loop loop specification allows you to omit sentence-1 so that you can declare or initialize variables elsewhere, and you can omit expression-3 so that you do not need to evaluate any expression after the completion of each loop.

The for loop is a special case. The while loop and the do loop are strict and require expression, but the for loop is a flexible iteration statement.

+7
source

I cannot give a definitive answer, but I assume that this is because in the case of the for loop there are three different expressions, each of which you may or may not need to be used depending on the loop.

In the case of if it would be pointless if there were no expression; he must always act as if the expression was true or false and, thus, was equivalent to only one of the sentences. In the case of while , there would be a meaningful interpretation of while () { } , which should have been evaluated before while (1) { } (giving you a loop from which you can exit break ), but I assume that the benefits are due to that this single character is not worth the trouble.

However, in the case of the for loop, there are three different expressions, each of which may or may not be needed. For example, if you want to initialize and increment something in each loop, but will use break to break out of the loop, you can write for (i = 0; ; ++i) , or if you just need a test and an increment, because that your variable is already initialized, you can write for (; i > 0; --i) . Given that each of these expressions may not be needed depending on your cycle, forcing you to fill in a placeholder for anything you don't use seems a bit cumbersome; and for consistency, instead of requiring a placeholder in one of them, all of them are optional, and the condition is considered a constant non-zero value if omitted.

Of course, it is sometimes easy to read too many intentions in a design decision. Sometimes the reason for this standard is simply how it was implemented in the first implementation, and everyone else just copied it. For example, see Rob Pike 's explanation of why files starting with . hidden in Unix; this was not due to a deliberate design decision, but simply because someone took a shortcut when writing ls and did not want to display the directory entries . and .. every time.

+3
source

Source: https://habr.com/ru/post/1445826/


All Articles