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
To make a C++ While Loop run indefinitely, the condition in the While loop has to be true forever. To make the condition always true, there are many ways.
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.
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.
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;
}
}
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
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
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
Conclusion
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.