# Why is for(;;) an infinite loop

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 04-26-2010
cyberfish
I think "for (; true; )" is an infinite loop makes sense because in general,
Code:

```for (a; b; c) { ... }```
is the same as
Code:

```a; while (b) { ... c; }```
in this case it would translate to
Code:

```while (true) { ... }```
And I don't see anything wrong with that.

However, with for(;; ), it would be
Code:

```while () { ... }```
which is syntax error.
• 04-26-2010
whiteflags
But you know better than that, so what are you really saying? Like I said, requiring all parts in a for loop is more trouble than it's worth.
• 04-26-2010
cyberfish
I don't think I know what you mean.

Are you saying it's an exception for convenience? and should otherwise be syntactically incorrect?
• 04-26-2010
whiteflags
Quote:

Are you saying it's an exception for convenience?
Well I don't think it matters what I say, that much is a fact. You will run into for loops without all three parts. Requiring an explicit condition might be more readable, but that doesn't explain why you didn't require the other two parts. After all, a for with a condition only is a fancy while. You could make an argument either way.

TL;DR: Opinions are like noses.
• 04-26-2010
cyberfish
I think the 3 statements are different.

We don't take the value of the first and last statements, and only use their side effects, so empty statements should be fine. However, we take the value of the middle one, so it makes sense to require it to be non-empty.

For example, in a function, you can write ";" (empty statement) on its own, because we don't take the value.

Code:

```int main() { ;;;; }```
However, we can't have something like
Code:

`int a = ;`
Because then we are taking the value of an empty statement.

That agrees with why while() and if() are illegal (taking values of empty expressions).

But for seems like an exception to this observation.
• 04-26-2010
whiteflags
You can write ";" on it's own because it it a sequence point. Other than what a sequence point means, there is no other reason why you can write that by itself. After ";" there should be no side effects with respect to the expressions in the preceding statement up to that point, even if that statement is a no-op statement (and "true" is a no-op expression). So int a = ; is just wrong. It has nothing to do with anything's value. (How can you say it is ; that is being misused and not the = operator?)

The for (;; ) thing is just for convenience. If you want to require a condition, there is no compelling argument for or against this, I think. It comes down to opinion on how the language's grammar needs to behave and I really don't see anything that couldn't be explained away.

Even while() and if() are only wrong because it is not conventional that they mean anything. Someone could come up with a meaning and it would be fine if it were ratified by the appropriate groups.

This is very similar to int foo(void); vs. int foo();
• 04-26-2010
kermit
This discussion reminds me of a similar one from a few years back that I wish had continued...
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12