Infinite "for" loop vs infinite "while" loop

Player's Avatar, Join Date: Aug 2007
Go4Expert Member
Hi guys.

Whats the difference between these two loops. From what i can see they both do exactly the same thing. Can anyone shed any light on this?

Thanks in advance
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
They're the same. for (x;y;z) { foo; } is equivalent to x; while (y) { foo; z; }. They're not exactly equivalent in later versions of the standard, for example for (int x=0; y; z), the scope of x is the for block and out of scope after the loop ends, whereas with int x; while (y) x is still in scope after the loop ends.
Another difference is that for interprets missing y as TRUE, whereas while must be supplied with an expression. for (;;) { foo; } is fine, but while() { foo; } isn't.