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
|
Go4Expert Member
|
|
| 27Jan2009,13:02 | #1 |
|
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
|
|
Mentor
|
![]() |
| 27Jan2009,13:46 | #2 |
|
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. |