In C++ you are allowed to have an empty condition inside the for-loop, for example as in for (;;)
or for (int x = 0;; ++x)
. But you can\'t do
As per the Document :
6.8.5.3 The for statement
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 identifiers 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.158)
- Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.
So, in case of for
loop, condition will be replaced by non-zero constant and no such implementation for while
loop.
Presumably, it's a side-effect of the fact that each given clause within the for-statement is optional. There's reasons why some for-loops wouldn't need an assignment; there's reasons why some others wouldn't need a condition; there's reasons why still others wouldn't need an increment. Requiring there to be some minimum number of them would be needlessly-added complexity.
The following is from the book Deep C Secrets:
Early C had no separate operators for & and && or | and ||. (Got that?) Instead it used the notion (inherited from B and BCPL) of "truth-value context": where a Boolean value was expected, after "if" and "while" and so forth, the & and | operators were interpreted as && and || are now; in ordinary expressions, the bitwise interpretations were used. It worked out pretty well, but was hard to explain. (There was the notion of "top-level operators" in a truth-value context.)
So there you have it, while()
would set the context for a truth value and can't be empty or implied for the same reason if() can't be empty, the mechanics of the early language expected them not to be.
In C++ you are allowed to have an empty condition inside the for-loop ... what's the argument behind not letting
while()
be an alias ofwhile(true)
?
First of all, C++ (and C# and many other languages) are the way they are for backwards compatibility with C, so let's talk about C.
Let's make some statements and draw a conclusion.
for
loop allows you to omit the condition.while
loop.while
loop does not allow the condition to be omitted; it is inconsistent.while
loop is inconsistent with the for
loop.And now the question is "what is that unobvious good reason?"
But the question only makes sense if the chain of logic described above is correct, which it is not. Of those statements, only #3 is actually true! The for
loop in C is a poor design; it's redundant, confusing for novices and its lexical conventions are inconsistent with the rest of the language. It provides almost zero additional representational power to the language over a straightforward while
loop.
There is no answer to your question; the correct response is rather to reject the premise of the question entirely. The for
loop only seems reasonable because it's a 40+ year old misfeature that most of us grew up with, so its quirks are second nature now; this is familiarity, not good design. Had we grown up without the for
loop, we wouldn't be adding it.
I think the main difference is between expressions and statements.
Sure, in C the two aren't rigorously distinguished, but I would say the for loop is:
for (statement; expression; statement) {
...
}
The expression is a boolean expression, while the statements are...statements which have side effects. Of course, C isn't semi-functional; you can put side effects in the middle expression, but it isn't idiomatic.
Yet sometimes you don't want an expression, but statements at the beginning and at each loop iteration is useful, so the middle expression is optional:
// Infinite counter
int i;
for (i = 0;;i++) {
...
}
This by definition means that the expression is considered always true
On the other hand, while
and if
can only take expressions. There is nothing very useful left (unlike the two useful statements left) if you omit that expression: nobody would write
// Idiot coding
if (true) {
...
}
because the point of an if
statement is to check whether some unknown is true or not!
Same thing with while
. The point of while
is to do something over and over as long as a condition in the form of an expression is true. It's kinda like an "iterated if
". This is why
while() {
...
}
is not considered logical and thus not allowed.
The difference between logical statements used in while() and for(;;) loops is that:
in case of for the statement defines condition of exiting the loop
in case of while() the statement defines condition of executing the loop
Assuming that, you see that to enter a loop, you need at least a condition of executing, not a condition of exiting.