Can someone explain why this isn't an infinite loop? (C++)

Click For Summary

Discussion Overview

The discussion revolves around a C++ programming question regarding the behavior of a for loop and its termination condition. Participants analyze why the loop does not result in an infinite loop and what the final value of the variable i is after the loop exits.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the loop's behavior, stating they see an infinite loop but the expected answer is 12.
  • Another participant suggests thinking like a compiler and ignoring whitespace, indicating that semicolons should be treated as ends of lines.
  • A participant proposes a corrected version of the code, arguing that the original code would yield a final value of 1 instead of 12.
  • One participant explains the loop's termination condition, noting that it ends when i reaches 10 or exceeds it.
  • Another participant clarifies that without braces, the for loop only includes the next statement, providing an equivalent code snippet with braces for clarity.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the interpretation of the loop's behavior, with differing views on the final value of i and the implications of the loop's structure.

Contextual Notes

There is a lack of clarity regarding the initial conditions and the specific behavior of the loop due to the absence of braces, which may affect participants' interpretations.

PNGeng
Messages
17
Reaction score
0
This was a question from my C++ midterm.

I see an infinite loop, but the correct answer is 12. Can anyone explain this?

Question: What is the value of i after the loop exits?
Code:
int i;
for(i=0; i < 10; i++)
i+=3;
i=1;
 
Technology news on Phys.org
Think like a compiler; ignore white space; treat semicolons as ends of lines.
 
I'm assuming that should be:
Code:
int i;
for(i=0; i < 10; i++)
i+=3;
i[b]+=[/b]1;
as otherwise the answer would be 1.

The best way is to step through the code by hand:
Code:
Iteration    Value     i<10?
1              3         yes    // starts at zero then adds 3
2              7         yes    // incremented by 1, then add 3
3              11         no    // same, then stops at next test
then add one to get 12.
 
Last edited:
The loop ends when i = 10 or i > 10. This condition is set in the line:
Code:
for(i=0; i < 10; i++)
 
Without any braces, the for loop only includes the next statement. The equivalent code with braces would be:

Code:
int i;

    for(i=0; i < 10; i++)
    {
        i+=3;
    }

    i=1;
 

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
18
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 29 ·
Replies
29
Views
10K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K