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

In summary, the value of i after the loop exits is 12. This is because the loop iterates three times, with the value of i increasing by 3 each time, until it reaches 10 and the loop stops. The final value of i is then incremented by 1, resulting in a final value of 12. The key to understanding this is to recognize that the for loop only includes the next statement, without any braces, so the value of i is not reset to 1 before the loop ends.
  • #1
PNGeng
17
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
  • #2
Think like a compiler; ignore white space; treat semicolons as ends of lines.
 
  • #3
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:
  • #4
The loop ends when i = 10 or i > 10. This condition is set in the line:
Code:
for(i=0; i < 10; i++)
 
  • #5
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;
 

What is an infinite loop?

An infinite loop is a programming construct where a set of instructions continues to be repeated indefinitely, without ever reaching a stopping point or condition.

Why is an infinite loop a problem?

An infinite loop can cause a program to crash, freeze, or consume excessive amounts of memory and processing power, making it difficult to terminate the program.

Why might someone think a code is an infinite loop?

There are a few common reasons why someone might think a code is an infinite loop: the code contains a conditional statement that is never met, the code has a logical error causing it to repeat endlessly, or the code is intended to run indefinitely.

How do you identify and fix an infinite loop?

To identify an infinite loop, you can use debugging tools to track the execution of the program and identify where it gets stuck. To fix an infinite loop, you can add a break condition, adjust the logic of the code, or use a loop control statement like a "break" or "continue" to exit the loop.

Why might someone mistake a program for an infinite loop when it is not?

Sometimes, a program might take a long time to execute due to complex calculations or a large dataset, leading someone to think it is an infinite loop. Additionally, a program may appear to be stuck in a loop due to a slow or unresponsive system, while in reality, the program is still executing as intended.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
991
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
7
Views
465
Back
Top