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; I think "for (; true; )" is an infinite loop makes sense because in general, Code: for (a; b; c) ...

  1. #16
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I think "for (; true; )" is an infinite loop makes sense because in general,
    Code:
    for (a; b; c) {
    ...
    }
    is the same as
    Code:
    a;
    while (b) {
    ...
    c;
    }
    in this case it would translate to
    Code:
    while (true) {
    ...
    }
    And I don't see anything wrong with that.

    However, with for(;; ), it would be
    Code:
    while () {
    ...
    }
    which is syntax error.

  2. #17
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    But you know better than that, so what are you really saying? Like I said, requiring all parts in a for loop is more trouble than it's worth.

  3. #18
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I don't think I know what you mean.

    Are you saying it's an exception for convenience? and should otherwise be syntactically incorrect?

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Are you saying it's an exception for convenience?
    Well I don't think it matters what I say, that much is a fact. You will run into for loops without all three parts. Requiring an explicit condition might be more readable, but that doesn't explain why you didn't require the other two parts. After all, a for with a condition only is a fancy while. You could make an argument either way.

    TL;DR: Opinions are like noses.

  5. #20
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I think the 3 statements are different.

    We don't take the value of the first and last statements, and only use their side effects, so empty statements should be fine. However, we take the value of the middle one, so it makes sense to require it to be non-empty.

    For example, in a function, you can write ";" (empty statement) on its own, because we don't take the value.

    Code:
    int main() {
    ;;;;
    }
    However, we can't have something like
    Code:
    int a = ;
    Because then we are taking the value of an empty statement.

    That agrees with why while() and if() are illegal (taking values of empty expressions).

    But for seems like an exception to this observation.

  6. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    You can write ";" on it's own because it it a sequence point. Other than what a sequence point means, there is no other reason why you can write that by itself. After ";" there should be no side effects with respect to the expressions in the preceding statement up to that point, even if that statement is a no-op statement (and "true" is a no-op expression). So int a = ; is just wrong. It has nothing to do with anything's value. (How can you say it is ; that is being misused and not the = operator?)

    The for (;; ) thing is just for convenience. If you want to require a condition, there is no compelling argument for or against this, I think. It comes down to opinion on how the language's grammar needs to behave and I really don't see anything that couldn't be explained away.

    Even while() and if() are only wrong because it is not conventional that they mean anything. Someone could come up with a meaning and it would be fine if it were ratified by the appropriate groups.

    This is very similar to int foo(void); vs. int foo();
    Last edited by whiteflags; 04-26-2010 at 04:42 PM.

  7. #22
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    This discussion reminds me of a similar one from a few years back that I wish had continued...

Page 2 of 2 FirstFirst 12
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