Infinite "for" loop vs infinite "while" loop

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

  1. Player

    Player New Member

    Joined:
    Aug 3, 2007
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice