6

I've written infinite loops like this numerous times over the years in both C and C++, but today is the first time I really thought about it -- why is it an infinite loop when the condition clause is empty? One would expect you'd have to write something like for(;true;); to get a valid infinite loop?

while(); doesn't compile nor does while(;);

Anyways, I like the for(;;); syntax and use it often, but is it a special case to treat an empty condition block as true or are there other cases in C or C++ where an empty condition expression is interpreted as true?

charunnera
  • 327
  • 4
  • 16
  • 6
    Because when a condition clause is empty it defaults to true, hence the loop. That's how C++ specifies `for` to behave. Mostly the same reason why water is wet. – Sam Varshavchik Mar 02 '17 at 03:14
  • `while();` is a compilation error so an empty condition clause does not always default to true. – charunnera Mar 02 '17 at 03:16
  • 5
    With what compiler and settings does `while(true)` test true against true? I can't manage to get anything other than an unconditional jump. – Benjamin Lindley Mar 02 '17 at 03:20
  • 1
    while needs a condition unlike for loop in C++, in while there is no default value. – vk3105 Mar 02 '17 at 03:21
  • 1
    Just because `for`'s condition may have a default does not require `while` to have a default too. Why does `for`'s condition have a default, and `while` doesn't? Who knows... Who cares... Not something to lose any sleep over. Just have to remember that this is how C++, then flip the page to the next chapter in your C++ book. – Sam Varshavchik Mar 02 '17 at 03:23
  • @charunnera "I've written infinite loop like this numerous times over the years in both C and C++, but today" I asked myself: "What was I doing?":) – Vlad from Moscow Mar 02 '17 at 03:25
  • Thanks guys. I'll just chalk it up to another special case. @VladfromMoscow, yes I was smiling as I wrote that...I'm sure I had this same question when I first saw the for(;;) syntax, figured out the answer and used the syntax without thinking...and then one day (today) someone asked me and I was like "good question".:) – charunnera Mar 02 '17 at 03:40
  • @BenjaminLindley, I only compiled for(;;); to asm and it used an unconditional jump. While researching this question, someone else mentioned that while(true); tested each loop and I bit. I removed that statement since I didn't actually verify it. Thanks for calling that out. – charunnera Mar 02 '17 at 03:50
  • 1
    Note [infinite loops are undefined behavior](http://stackoverflow.com/q/3592557/1708801) outside of the for case and if the loop contains observable behavior. C11 makes an exception for [controlling expressions that are constant expressions](http://stackoverflow.com/a/28681034/1708801) – Shafik Yaghmour Mar 02 '17 at 04:56

2 Answers2

12

The C Standard explicitly describes this behavior of for loops:

C11 Draft Standard §6.8.5.3 2

Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

Similarly, for C++:

C++14 Draft Standard §6.5.3 2

Either or both of the condition and the expression can be omitted. A missing condition makes the implied while clause equivalent to while(true).

ad absurdum
  • 15,925
  • 4
  • 28
  • 49
  • 1
    Thanks for pointing there is an explicitly defined behavior for omitting the expression. – charunnera Mar 02 '17 at 03:34
  • 1
    This question has been asked many times before and has many duplicates, including highly upvoted ones. You are a member with a lot of reputation, and thus have been given privilege and understanding to do the following. Improving StackOverflow by reducing duplicates by voting to close as a duplicate instead of answering. Such action will greatly improve the community at the negligible disadvantage to yourself. Thank you. – 2501 Mar 02 '17 at 07:45
  • @2501-- I agree that this question is a duplicate; in fact I knew that it was a duplicate of the question that you linked to, since that was my first question on SO! When I asked that question, I found no other questions that asked about the _nature_ of this behavior (a result of the evaluation model or defined behavior). When I answered, it seemed to me that one duplicate is not a bad thing. Of course, the answer turns out to be mundane. – ad absurdum Mar 02 '17 at 14:41
4

The behaviors are defined by the language (C++). For the for loop, the condition part is optional:

Syntax

formal syntax:

attr(optional) for ( init-statement condition(optional) ; iteration_expression(optional) ) statement

and (emphasis mine)

The above syntax produces code equivalent to:

{
    init_statement 
    while ( condition ) { 
        statement 
        iteration_expression ; 
    }
}

Except that

3) Empty condition is equivalent to while(true)

For while loop, the condition part is necessary, it can't be ommitted.

Syntax

attr(optional) while ( condition ) statement
Community
  • 1
  • 1
songyuanyao
  • 147,421
  • 15
  • 261
  • 354
  • Thank you for your response – charunnera Mar 02 '17 at 03:35
  • This question has been asked many times before and has many duplicates, including highly upvoted ones. You are a senior member with a lot of reputation, and thus have been given privilege and understanding to do the following. Improving StackOverflow by reducing duplicates by voting to close as a duplicate instead of answering. Such action will greatly improve the community at the negligible disadvantage to yourself. Thank you. – 2501 Mar 02 '17 at 07:45