Thread: for( ; ;)

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    for( ; ;)

    Does for( ; test for
    expr !=0 or expr == 0?

    Chad

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Well, for(;; ) is technically always true, so it doesn't matter. But in general, conditional tests by default test for != 0. That's why
    Code:
    if ( x ) {
      /* Stuff */
    }
    Executes the block if x isn't 0.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    probably depends on the compiler, but VC++ 2005 Express does not make any tests, it just creates an infinite loop with a jump at the bottom back to the top of the loop. No test is necessary. As if you had written this:
    Code:
    int main()
    {
    loop:
       // do some code here
       goto loop;
    }
    Last edited by Ancient Dragon; 01-20-2006 at 10:34 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The C Standard says that an "omitted expression-2 is replaced by a nonzero constant", where expression-2 refers to the expression that controls the for loop.

    So you might say that it tests for expr == 1, but then the wording of the standard is effectively "leaving out expression-2 creates an infinite loop", so no comparison is actually required in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed