Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Infinite "for" loop vs infinite "while" loop (http://www.go4expert.com/forums/infinite-loop-vs-infinite-loop-t15951/)

Player 27Jan2009 13:02

Infinite "for" loop vs infinite "while" loop
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 :)

xpi0t0s 27Jan2009 13:46

Re: Infinite "for" loop vs infinite "while" loop
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.

All times are GMT +5.5. The time now is 07:51.