In this C++ tutorial, you will learn how an infinite for loop works, how to create one intentionally, how an accidental infinite loop occurs, and how to stop or exit the loop safely.
Infinite For Loop in C++
To make a C++ For Loop run indefinitely, its condition must continue to evaluate to true. You can do this explicitly with true, use an expression that always evaluates to true, or omit the condition entirely.
An infinite loop can also happen accidentally. For example, if the loop condition depends on a variable that is never updated, the condition may remain true forever.
C++ Infinite For Loop Syntax Using for (;;)
A common way to write an intentional infinite for loop in C++ is to omit the initialization, condition, and update expressions.
for (;;) {
// statements
}
The two semicolons are still required because they separate the three parts of a for statement. When the condition is omitted, the loop does not have a false condition that would terminate it. It therefore continues until control leaves the loop in another way, such as with a break statement, return, or program termination.
Flowchart of an Infinite For Loop in C++
Following is the flowchart of infinite for loop in C++.
As long as the condition never becomes false, execution repeatedly returns to the loop body. The control therefore does not proceed to the statement after the loop unless some statement inside the loop explicitly exits it.
C++ Infinite For Loop Examples
1. Infinite For Loop with condition=true
For Loop condition is a boolean expression that evaluates to true or false. So, instead of providing an expression, we can provide the boolean value true, in place of condition, and the result is an infinite for loop.
C++ Program
#include <iostream>
using namespace std;
int main() {
for (; true; ) {
cout << "hello" << endl;
}
}
Output
hello
hello
hello
hello
Note: You will see the string hello print to the console infinitely, one line after another. If you are running from command prompt or terminal, to terminate the execution of the program, enter Ctrl+C from keyboard. If you are running the program from an IDE, click on stop button provided by the IDE.
2. Infinite For Loop with a Condition That Always Evaluates to True
Instead of giving true boolean value for the condition in for loop, you can also give a condition that always evaluates to true. For example, the condition 1 == 1 or 0 == 0 is always true. No matter how many times the loop runs, the condition is always true and the for loop will run forever.
C++ Program
#include <iostream>
using namespace std;
int main() {
for (; 1 == 1; ) {
cout << "hello" << endl;
}
}
Output
hello
hello
hello
hello
.
.
Because 1 == 1 cannot become false while this program runs, the loop has no condition-based exit. In production code, for (;;) or for (; true; ) usually communicates an intentional infinite loop more clearly than an artificial comparison such as 1 == 1.
3. Accidental Infinite For Loop When the Control Variable Is Not Updated
These type of infinite for loops may result when you forget to update the variables participating in the condition.
In the following example, we have initialized variable i to 0 and would like to print a string to console while the i is less than 10. Typically, in the following example, one would increment i in the update section of for loop statement, to print hello 10 times. But, if we have not updated i in neither the for loop body nor for loop update section, i would never change. This could make the loop an infinite while loop.
C++ Program
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; ) {
cout << "hello" << endl;
}
}
Output
hello
hello
hello
hello
Here, i starts at 0. Since i is never incremented, the expression i < 10 remains true. This is an accidental infinite for loop.
If the intention is to execute the loop ten times, add an update expression such as i++.
for (int i = 0; i < 10; i++) {
cout << "hello" << endl;
}
4. Infinite for (;;) Loop with a break Condition
An infinite loop does not always mean that the program can never leave the loop. A common pattern is to create an intentionally unbounded loop and use break when an exit condition is reached.
#include <iostream>
using namespace std;
int main() {
int number = 1;
for (;;) {
cout << number << endl;
if (number == 5) {
break;
}
number++;
}
cout << "Loop ended" << endl;
return 0;
}
Output
1
2
3
4
5
Loop ended
The for (;;) statement itself has no terminating condition. However, when number becomes 5, the break statement transfers control to the first statement after the loop.
How to Stop an Infinite For Loop in C++
If an infinite loop is intentional, the program normally needs a defined way to leave it when some condition is satisfied. Depending on the program, that may be a break statement, a return from the current function, or another normal program-termination path.
- Use
break: exits the nearest enclosing loop. - Use
return: exits the current function, which also ends the loop. - Correct the loop condition: if the loop is accidental, make sure the condition can eventually become false.
- Update the control variable: check that increment, decrement, or other state changes actually occur.
- Stop a program that is already stuck: in a terminal,
Ctrl+Ccommonly interrupts a foreground program. In an IDE, use its stop or terminate control.
Common Causes of Accidental Infinite For Loops in C++
When a for loop runs longer than expected, inspect the condition and every value used by that condition. Typical causes include the following.
- The loop counter is never updated.
- The counter is updated in the wrong direction, such as decrementing a value while testing whether it is less than an upper bound.
- The update statement changes a different variable from the one used in the condition.
- A value modified inside the loop causes the condition to remain true instead of moving toward termination.
- An intended
breakcondition is never reached.
Example of Updating a For Loop Counter in the Wrong Direction
Consider a loop that starts with i = 0, checks i < 10, but decrements i. The value moves farther away from 10, so the condition continues to be true for ordinary values reached by the loop.
for (int i = 0; i < 10; i--) {
// i moves in the wrong direction
}
For a counting loop intended to progress from 0 toward 10, the update should normally increase i, for example with i++.
Intentional Infinite For Loop vs Accidental Infinite For Loop
An intentional infinite loop is written deliberately when the number of iterations is not known in advance and termination is controlled from inside the loop or by the surrounding program. An accidental infinite loop is a logic error in which the programmer expected the loop condition to eventually become false, but it does not.
| Loop type | Typical form | How it ends |
|---|---|---|
| Intentional infinite loop | for (;;) or for (; true; ) | An explicit exit such as break, return, or program termination |
| Accidental infinite loop | A condition remains true because of incorrect loop logic | It must usually be fixed by correcting the condition or update logic |
C++ Infinite For Loop Editorial QA Checklist
- Verify that every intentional infinite-loop example clearly shows why its condition never becomes false.
- Check that examples using
for (;;)retain both required semicolons. - For accidental-loop examples, confirm that the missing or incorrect update is explicitly identified.
- For loops that use
break, confirm that the break condition can actually be reached. - Do not describe a finite loop with an internal
breakas permanently infinite; distinguish an unbounded loop structure from actual runtime behavior. - When showing terminal interruption, use
Ctrl+Conly as a way to stop the running program, not as a substitute for correcting faulty loop logic.
Summary of Infinite For Loops in C++
In this C++ Tutorial, we learned that a for loop becomes infinite when its condition never becomes false. We used true, an always-true expression, and the common for (;;) form to create intentional infinite loops. We also saw how a missing update can create an accidental infinite loop, and how break, correct counter updates, and proper termination conditions can control or fix such loops.
TutorialKart.com