1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Infinite "for" loop vs infinite "while" loop

Discussion in 'C' started by Player, Jan 27, 2009.

  1. Player

    Player New 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 :)
  2. xpi0t0s

    xpi0t0s 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.
    shabbir likes this.

Share This Page