In C++, a loop is a part of code that is executed repetitively until the given condition is satisfied. An infinite loop is a loop that runs indefinitely, without any condition to exit the loop.
In this article, we will learn about infinite loops in C++, their types and causes, and their applications.
Here’s what we’ll cover:
What is an Infinite Loop in C++?
An infinite loop is any loop in which the loop condition is always true, leading to the given block of code being executed an infinite number of times. They can also be called endless or non-terminating loops, which will run until the end of the program’s life.
Infinite loops are generally accidental and occur due to some mistake by the programmer. But they are pretty useful, too, in different kinds of applications, such as creating a program that does not terminate until a specific command is given.
Types of Infinite Loops in C++
There are several ways to create an infinite loop in C++, using different loop constructs such as while, for, and do-while loops. Here, we will explore each method and provide examples.
-
Infinite While Loops
-
Infinite For Loops
-
Infinite do-while Loops
1. Infinite Loop using While Loop
This is the most popular type of while loop due to its simplicity. You just pass the value that will result in true as the condition of the while loop.
Syntax:
while(1)
or
while(true)
Example Code:
<span class="hljs-comment">// Example of Infinite loop in C++ using for loop</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Infinite loop using while</span>
<span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"This is an infinite loop."</span> << <span class="hljs-built_in">endl</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
………………….
Infinite Loop using For Loop
In a for loop, if we remove the initialization, comparison, and update conditions, then it will result in an infinite loop.
Syntax:
for(;;)
Example Code:
<span class="hljs-comment">//Example of Infinite loop in C++ using for loop</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Infinite loop using for loop</span>
<span class="hljs-keyword">for</span> (;;) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"This is an infinite loop."</span> << <span class="hljs-built_in">endl</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
……………………….
Infinite Loop using do-while Loop
Just like the other two loops discussed above, we can also create an infinite loop using a do-while loop. Although this loop is not preferred much due to its longer syntax.
Syntax:
do{
}while(1)
Example Code:
<span class="hljs-comment">// Infinite loop in C++ using do-while loop</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// infinite do-while loop</span>
<span class="hljs-keyword">do</span> {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"This is an infinite loop."</span> << <span class="hljs-built_in">endl</span>;
} <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
……………………….
Common Causes of Accidental Infinite Loops in C++
Infinite loops can be both intentional and accidental. Accidental infinite loops are those which were not intended by the programmer but are caused due to some error in the program.
Following are some of the errors that may cause infinite loops in your programs unintentionally:
1. Missing Update Statements
Infinite loops are caused when you forget to add an update condition inside the loop, which will terminate the loop in the future. The following program illustrates such a scenario:
Example Code:
<span class="hljs-comment">// Infinite loop caused due to missing update statement</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>;
<span class="hljs-keyword">while</span> (i < <span class="hljs-number">5</span>) {
<span class="hljs-built_in">cout</span> << i <<<span class="hljs-built_in">endl</span>;
<span class="hljs-comment">// Missing update: i++;</span>
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
3
3
3
3
3
3
3
……………………
To fix the above code, we can add an update condition inside the loop like this:
<span class="hljs-comment">// fixed code</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span> ;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>;
<span class="hljs-keyword">while</span> (i < <span class="hljs-number">5</span>) {
<span class="hljs-built_in">cout</span> << i << <span class="hljs-built_in">endl</span>;
i++; <span class="hljs-comment">// add the condition</span>
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span> ;
}
Output:
3
4
Incorrect Loop Conditions
The conditions mentioned inside the loop body are crucial to terminate a loop. An incorrect loop condition can result in an infinite loop. The following program illustrates such a scenario:
Example Code:
<span class="hljs-comment">// Infinite loop caused due to incorrect loop conditions</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>;
<span class="hljs-keyword">while</span> (i >= <span class="hljs-number">0</span>) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Hello AnshuAyush "</span> << <span class="hljs-built_in">endl</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
Hello AnshuAyush
Hello AnshuAyush
Hello AnshuAyush
Hello AnshuAyush
Hello AnshuAyush
……………………..
To fix the above code, we can update i
inside the loop to eventually make the condition false:
<span class="hljs-comment">// fixed code </span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span> ;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>;
<span class="hljs-keyword">while</span> (i >= <span class="hljs-number">0</span>) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Hello AnshuAyush"</span> << <span class="hljs-built_in">endl</span>;
i--; <span class="hljs-comment">// loop will stop</span>
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span> ;
}
Ouptut:
Hello AnshuAyush
Hello AnshuAyush
Hello AnshuAyush
Logical Errors in the Loop
In many scenarios, infinite loops are caused by small logical errors in the code. The following program illustrates such a scenario:
Example Code:
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i ><span class="hljs-number">2</span>; i += <span class="hljs-number">2</span>) {
<span class="hljs-built_in">cout</span> <<<span class="hljs-string">"This is an infinite loop"</span> << <span class="hljs-built_in">endl</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
……………………….
To fix the above code, we can either use a decreasing condition or use an incrementing loop condition.
Decreasing condition:
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i > <span class="hljs-number">0</span>; i--) {
<span class="hljs-built_in">cout</span> <<<span class="hljs-string">"This is NOT an infinite loop"</span> << <span class="hljs-built_in">endl</span>;
}
Increasing condition:
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i < <span class="hljs-number">10</span>; i += <span class="hljs-number">2</span>) {
<span class="hljs-built_in">cout</span> <<<span class="hljs-string">"Loop will end when i reaches 10"</span> << <span class="hljs-built_in">endl</span>;
}
Applications of Infinite Loops in C++
Infinite loops do not only occur by accident, as I mentioned above. You can also create them on purpose for different use cases. The following are some of the common applications where you might use infinite loops intentionally:
-
Event loops: Many Graphical User Interfaces (GUIs) use infinite loops to keep the program running and responsive to user actions.
-
Server applications: Web servers use infinite loops to continuously listen to client connections or requests.
-
Embedded systems: Embedded systems, such as microcontrollers, frequently use infinite loops as their main program loops to continuously respond to external events.
-
User inputs: Infinite loops are also used to wait for valid user inputs. The loop keeps running until a valid input is provided by the user. We’ll look at an example of this one.
Using Infinite Loops to Take User Input in C++
Infinite loops are commonly used in scenarios where a program needs to continuously take user input until a specific condition is met, such as exiting the program or getting a valid user input. The following program demonstrates how we can take user input from the user until a specific condition is met:
Example Code:
<span class="hljs-comment">// C++ Program to take user input from users using infinite loops</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-built_in">string</span> input;
<span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Enter a command (type 'exit' to quit): "</span>;
getline(<span class="hljs-built_in">cin</span>, input);
<span class="hljs-keyword">if</span> (input == <span class="hljs-string">"exit"</span>) {
<span class="hljs-comment">// Exit the loop if the user types 'exit'</span>
<span class="hljs-keyword">break</span>;
}
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"You entered: "</span> << input << <span class="hljs-built_in">endl</span>;
<span class="hljs-comment">// Process the input</span>
}
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Program exited."</span> << <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
Enter a command (type ‘exit’ to quit): Anshu
You entered: Anshu
Enter a command (type ‘exit’ to quit): Ayush
You entered: Ayush
Enter a command (type ‘exit’ to quit): exit
Program exited.
Conclusion
Infinite loops aren’t always dangerous. They can be very useful when used with proper control, like break statements or condition checks. But if you use them carelessly, they can crash your program.
So just make sure you check your loop conditions and test your code using print statements between the programs to discover any unexpected behavior. In sum, infinite loops can be very powerful when handled carefully but can be very risky if left unchecked.
If you are beginner in C++, I’ve covered many programming topic in detail on the TutorialsPoint platform, where I regularly write about beginner-friendly programming concepts.
📚 Other C++ tutorials you may like:
-
Efficient Binary Search in C++: First and Last Index of an Element
-
TCS NQT Asked Question: Find Sum of Array Elements Between Two Indices in C++
Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & MoreÂ