tags:

views:

834

answers:

8

If my understanding is correct, they do exactly the same thing. Why would anyone use for the "for" variant? Is it just taste?

Edit: I suppose I was also thinking of for (;;).

+7  A: 

Yes, it is just taste.

Oren Shemesh
I don't see why this was voted down. It's not a great answer, but it answers the question correctly.
Paul Tomblin
+5  A: 

I've never seen for (;true;). I have seen for (;;), and the only difference seems to be one of taste. I've found that C programmers slightly prefer for (;;) over while (1), but it's still just preference.

mipadi
mmm... maybe (;;) is what I was thinking of.
Sydius
but you shave an entire byte off the source code!
Jimmy
I've always used while(1). I worked with a guy who preferred do{...}while(1); because he said it's clearer that you always start the loop, but might exit it part way through. He didn't convince me.
Paul Tomblin
if i always need to run once, i'll always do a do..while loop
warren
while (true) _does_ make it clear that you run it almost once. true is always true :)
Daniel Daranas
+1  A: 

It's in case they plan to use a real for() loop later. If you see for(;true;), it's probably code meant to be debugged.

Jekke
+2  A: 

Some compilers (with warnings turned all the way up) will complain that while(true) is a conditional statement that can never fail, whereas they are happy with for (;;).

For this reason I prefer the use of for (;;) as the infinite loop idiom, but don't think it is a big deal.

Rob Walker
+32  A: 
for (;;)

is often used to prevent a compiler warning:

while(1)

or

while(true)

usually throws a compiler warning about a conditional expression being constant (at least at the highest warning level).

Stefan
+3  A: 

Not an answer but a note: Sometimes remembering that for(;x;) is identical to while(x) (In other words, just saying "while" as I examine the center expression of an if conditional) helps me analyze nasty for statements...
For instance, it makes it obvious that the center expression is always evaluated at the beginning of the first pass of the loop, something you may forget, but is completely unambiguous when you look at it in the while() format.

Sometimes it also comes in handy to remember that

a;
while(b) {
   ...
   c;
}

is almost (see comments) the same as

for(a;b;c) {
    ...
}

I know it's obvious, but being actively aware of this relationship really helps you to quickly convert between one form and the other to clarify confusing code.

Bill K
Good explanation. I think some C++ developers might be surprised that your last examples are equivalent.
Daniel Daranas
They're not _quite_ the same: a continue statement causes 'c' to get executed in the 'for' variant, whereas 'c' does not get executed in the 'while' variant.
Adam Rosenfield
@adam Good point--I knew there was something subtle I was forgetting, I was actually a little afraid it was less subtle than that :)
Bill K
Another difference is that c in your while example might reference variables of the body. It should rather be{ a; while(b) { { ... } c; }}
jpalecek
A: 

An optimizing compiler should generate the same assembly for both of them -- an infinite loop.

Arcane
And a **really** optimising compiler will unroll that loop...
j_random_hacker
A: 

The compiler warning has already been discussed, so I'll approach it from a semantics stand-point. I use while(TRUE) rather than for(;;) because in my mind, while(TRUE) sounds like it makes more sense than for(;;). I read while(TRUE) as "while TRUE is always TRUE". Personally, this is an improvement in the readability of code.

So, Zeus forbid I don't document my code (this -NEVER- happens, of course) it stays just a little bit more readable than the alternative.

But, overall, this is such a nit-picky thing that it comes down to personal preference 99% of the time.

Matthew