Thread: Why is for(;;) an infinite loop

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    Why is for(;;) an infinite loop

    Random question of the day -

    Why is "for( ;; )" an infinite loop?

    The first and third parts don't matter, so does that mean an empty expression evaluates to true?

    How come "if ()" and "while ()" aren't allowed then?

    Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cyberfish View Post
    The first and third parts don't matter, so does that mean an empty expression evaluates to true?
    Yes. In for loops, at least.

    Quote Originally Posted by cyberfish View Post
    How come "if ()" and "while ()" aren't allowed then?
    Because the designers didn't like that, I guess ;-). Empty doesn't always evaluate to true, but the designers of C added an exception to the conditional statement in the for loop. Just because they considered that a lot more useful than "while()", I guess.
    And I agree, they were right on this decision.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Why shouldn't it be?

    Soma

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I've always used
    Code:
    while (true)
    when I want an infinite loop because it makes sense to me. It seems like most people prefer
    Code:
     for (;;)
    though.

    Why shouldn't it be?
    Why shouldn't what be what?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I, personally, prefer while(true). But I guess it's just a matter of style.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cyberfish
    Why shouldn't what be what?
    Why shouldn't for (;;) denote an infinite loop?

    Quote Originally Posted by EVOEx
    I, personally, prefer while(true). But I guess it's just a matter of style.
    I've read an ancient text on optimisation in C that recommended for (;;) over while (1) because compilers back then supposedly might not recognise while (1) as an infinite loop, and end up generating less efficient code.
    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

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Am I the only person to give the "do" a little love?

    Soma

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by phantomotap
    Am I the only person to give the "do" a little love?
    Sorry, not married

    But no, not for (controlled) infinite loops.
    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

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    I've read an ancient text on optimisation in C that recommended for (;;) over while (1) because compilers back then supposedly might not recognise while (1) as an infinite loop, and end up generating less efficient code.
    Yes, I read about that too. But I'm not going to write code I consider less clear because there exist some fairly bad compilers that aren't able to work with it? No doubt that some other compilers won't be able to do other things properly that are fairly common. I'm not going to bother to keep up with all of them.
    Should it ever be required that I'd write code for such a compiler, I'd change those things in the end, if required...

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Sorry, not married
    >_<

    GM > You've dealt `Soma' +3(1d4) point(s) of damage.

    Soma

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    My personal favorite:

    Code:
    o:goto o;
    Last edited by Sebastiani; 04-26-2010 at 09:52 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    o_O
    : 
    {
         //...
         goto o_O;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    I've read an ancient text on optimisation in C that recommended for (;;) over while (1) because compilers back then supposedly might not recognise while (1) as an infinite loop, and end up generating less efficient code.
    Another more current reason is that in some compilers you can get a warning about a loop expression being constant in while (true) etc, which is not a problem with for(;

    Quote Originally Posted by phantomotap View Post
    Am I the only person to give the "do" a little love?
    "do while" is mostly a good choice as it is logically the simplest form of loop, obviously resulting in just a single branch instruction. However it also falls prey to generating a warning on some compilers.
    Last edited by iMalc; 04-26-2010 at 01:23 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    But the question remains... why is for(;; ) infinite loop .

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well let's consider the available options:

    for(;; ) means loop forever
    for(;; ) means don't loop
    for(;; ) is a grammar mistake

    Maybe you feel that all three parts should have expressions but that would be bad for loops without meaningful initialization or update steps, like, I don't know, event driven loops. And if you're not going to require all three, then the question becomes "why is for(; true; ) an infinite loop?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using while produces an infinite loop...
    By UCF43 in forum C Programming
    Replies: 4
    Last Post: 04-01-2010, 04:47 PM
  2. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  3. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  4. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  5. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM