3

I ran across this for ( ; ; ) {}

A quick cursory search lead me nowhere in finding out what this means. It looks like another thread might be handling terminating this. Is this equivalent to 'while (TRUE) {}' ?

13
  • 2
    while(TRUE){} isn't valid C++. Both while(true){} and for(;;){} are undefined behaviour. Commented Oct 7, 2013 at 22:30
  • 10
    @KerrekSB How exactly are either of those undefined behavior? Commented Oct 7, 2013 at 22:31
  • 1
    @Kevin open-std.org/jtc1/sc22/wg14/www/docs/n1528.htm Commented Oct 7, 2013 at 22:33
  • @Kevin: 1.10/24: "The implementation may assume that every thread teminates." Commented Oct 7, 2013 at 22:36
  • @KerrekSB: That refers to 6.8.5p5, which says: "An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate.". That doesn't apply to for (;;) {}. (A footnote says that for (;;) is treated as having a constant controlling expression.) Commented Oct 7, 2013 at 22:42

2 Answers 2

10

This is an infinite loop. Any of the three parts of a for loop (initialization, condition and increment) can be missing. Specifically, if the condition in a for loop is missing, it is treated as being true. So it is equivalent to while(1) { ... }.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes they are equivalent in functionalities.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.