Why is for(;;) an infinite loop

This is a discussion on Why is for(;;) an infinite loop within the C++ Programming forums, part of the General Programming Boards category; Random question of the day - Why is "for( ;; )" an infinite loop? The first and third parts don't ...

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

    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
    4,415
    O_o

    Why shouldn't it be?

    Soma

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    4,415
    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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    4,415
    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 10: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,308
    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 02: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,183
    But the question remains... why is for(;; ) infinite loop .

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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?"

Page 1 of 2 12 LastLast
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, 05:47 PM
  2. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  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, 01:09 PM
  5. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21