In this C++ tutorial, you will learn what an infinite While loop is, how to create an infinite While loop, with the help of example programs.
C++ Infinite While Loop
A C++ While Loop becomes infinite when its condition continues to evaluate to true and execution never reaches a statement that exits the loop. An infinite loop may be intentional, such as a program that repeatedly waits for input, or accidental, such as when a loop control variable is not updated.
In C++, an infinite while loop is commonly written with while (true). A condition that is permanently true, such as 1 == 1, can produce the same behavior. Bugs involving loop-control variables and continue statements can also make a loop run indefinitely.
C++ Infinite While Loop Syntax
The following is the basic syntax for an intentional infinite while loop in C++.
while (true) {
// statements to repeat
}
The condition true never becomes false by itself. Therefore, the loop keeps starting another iteration unless its execution is stopped explicitly, for example with a break statement.
Flowchart – Infinite While Loop
Following is the flowchart of infinite while loop in C++.
As the condition is never going to be false, the control never comes out of the loop, and forms an Infinite Loop as shown in the above diagram.
C++ Infinite While Loop Examples
1. Infinite While loop with true for condition
While 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 while loop.
C++ Program
#include <iostream>
using namespace std;
int main() {
while (true) {
cout << "hello" << endl;
}
}
Output
hello
hello
hello
hello
Note: You will see the string hello print to the console infinitely. If you are running from command prompt or terminal, to terminate the execution of the program, enter Ctrl+C from keyboard.
The output above shows only the first few lines. Because the loop condition is always true, the program continues printing until execution is interrupted or otherwise terminated.
Instead of true, you can also give a non-zero integer.
C++ Program
#include <iostream>
using namespace std;
int main() {
while (1) {
cout << "hello" << endl;
}
}
In a Boolean context, the non-zero integer value 1 converts to true, so while (1) also forms an infinite loop. In modern C++ code, while (true) usually states the intent more directly.
2. Infinite While loop with condition that always evaluates to true
Instead of giving true boolean value or a non-zero integer in place of while loop condition, 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.
C++ Program
#include <iostream>
using namespace std;
int main() {
while (1 == 1) {
cout << "hello" << endl;
}
}
Output
hello
hello
hello
hello
The comparison 1 == 1 produces true every time the condition is evaluated. Since nothing in the loop changes that expression, the loop has no condition-based termination point.
3. Infinite While loop with no update to control variables
These type of infinite while loops may result when you forget to update the variables participating in the while loop 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 inside while loop body, to print hello 10 times. But, if we forget this update statement in the while body, i is never changed. This could make the loop an infinite while loop.
C++ Program
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 10) {
cout << "hello" << endl;
}
}
Output
hello
hello
hello
hello
Here, i starts at 0 and never changes. Therefore, i < 10 remains true on every condition check. This is a common form of accidental infinite loop.
4. Infinite While loop while working with continue statement
This also is a typical scenario where we use a continue statement in the while loop body, but forget to modify the control variable.
Extending the previous example, consider we have added the increment statement. But, now we have an additional functionality that the loop has to continue when i becomes 5. When continue statement is executed, the executes goes to the while condition, and the increment statement is not executed. This results in the scenario where i is never incremented again. And the while loop executes infinitely.
C++ Program
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 10) {
if (i == 5) {
continue;
}
cout << "hello" << endl;
i++;
}
}
Output
hello
hello
hello
hello
hello
The loop prints hello while i is 0 through 4. Once i reaches 5, continue skips the remaining statements in the loop body. The statement i++ is therefore never reached again, leaving i equal to 5 indefinitely.
How to End an Infinite While Loop in C++ with break
An intentional infinite loop can still have a controlled exit. A common C++ pattern is to use while (true) and execute break when a stopping condition is reached.
#include <iostream>
#include <string>
using namespace std;
int main() {
string command;
while (true) {
cout << "Enter a command (quit to stop): ";
cin >> command;
if (command == "quit") {
break;
}
cout << "You entered: " << command << endl;
}
cout << "Loop ended" << endl;
return 0;
}
In this example, the condition of the while statement itself never becomes false. Instead, entering quit makes the program execute break. Control then moves to the first statement after the loop.
How to Stop a Running C++ Infinite Loop
If a C++ program is already running indefinitely in a terminal or command prompt, Ctrl+C commonly sends an interrupt that terminates the process. Development environments such as IDEs may also provide a Stop or Terminate control for a running program.
Stopping the process is useful while debugging, but it does not correct an accidental infinite loop. If the loop is expected to finish naturally, inspect its condition and the statements that update the values used by that condition.
How to Fix an Accidental Infinite While Loop in C++
For a condition-controlled while loop to terminate normally, the program must eventually change its state so that the loop condition becomes false. For the earlier example using i < 10, incrementing i on every iteration fixes the problem.
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 10) {
cout << "hello" << endl;
i++;
}
return 0;
}
Now i increases after every iteration. When it becomes 10, the condition i < 10 evaluates to false and the loop ends.
Fixing a C++ Infinite While Loop Caused by continue
With a continue statement, pay attention to any update that appears later in the loop body. continue immediately starts the next iteration, so those later statements are skipped.
One way to correct the earlier continue example is to update i before executing continue.
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 10) {
if (i == 5) {
i++;
continue;
}
cout << i << endl;
i++;
}
return 0;
}
Output
0
1
2
3
4
6
7
8
9
When i is 5, it is incremented to 6 before continue runs. As a result, the loop keeps progressing toward the condition i < 10 becoming false.
Common Causes of Infinite While Loops in C++
- A condition that is deliberately always true:
while (true)andwhile (1)continue indefinitely unless another statement exits the loop. - A loop variable that is never updated: if
istarts below a limit and never changes, a condition such asi < 10remains true. - An update in the wrong direction: increasing a variable when it needs to decrease, or decreasing it when it needs to increase, may prevent the termination condition from being reached.
- A continue statement that skips an update: if the update is below
continue, that update is not executed on the continued path. - A stopping condition that cannot be reached: the values changed inside the loop may never produce the state required to make the condition false or execute
break.
Checking C++ While Loop Code for Infinite-Loop Bugs
- Identify every variable used in the
whilecondition and verify where each variable changes. - Trace whether those changes actually move the condition toward
false. - Check every
continuepath to make sure it does not bypass a required increment, decrement, input operation, or state update. - For an intentional
while (true)loop, verify that itsbreakcondition is reachable. - If the loop depends on user input, verify that the input can produce the value or state required to exit.
- When debugging an unexpected infinite loop, print or inspect the values used by the condition on successive iterations to see which value is not changing as expected.
C++ Infinite While Loop Summary
A brief recap of all the above examples in this C++ Tutorial, you may note that to write an Infinite While Loop in C++, we have to make sure that the condition in while statement has to be always true.
An intentional C++ infinite loop is commonly written using while (true) and can be given a controlled exit with break. Accidental infinite loops often result from a missing or incorrect control-variable update, an unreachable stopping condition, or a continue statement that skips an update. When a program is already stuck in a terminal, Ctrl+C can be used to stop its execution while you inspect and correct the loop logic.
TutorialKart.com