Infinite Loop in C++

Last Updated : 16 Jun, 2026

An infinite loop is a loop that executes continuously because its termination condition never becomes false. It can occur intentionally in applications that require continuous execution or unintentionally due to programming errors.

  • Infinite loops can be created using while, for, and do-while loops.
  • They are commonly used in servers, event-driven applications, and embedded systems.

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 Loop using While Loop

A while loop becomes infinite when its condition always evaluates to true.

Syntax:

while(1)
or
while(true)

C++
#include <iostream>
using namespace std;

int main() {
    for (;;) {
        cout << "This is an infinite loop." << endl;
    }
    return 0;
}

Output

This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
...........

Explanation: Since the loop condition never becomes false, the loop continues executing indefinitely.

Infinite Loop using For Loop

A for loop becomes infinite when all three loop expressions (initialization, condition, and update) are omitted.

Syntax

for (;;) {
// statements
}

C++
#include <iostream>
using namespace std;

int main() {
    for (;;) {
        cout << "This is an infinite loop." << endl;
    }
    return 0;
}

Output

This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
.......

Explanation: Since no termination condition is provided, the loop executes forever.

Infinite Loop using do-while Loop

A do-while loop becomes infinite when its condition always evaluates to true.

Syntax

do {
// statements
} while (true);

C++
#include <iostream>
using namespace std;

int main() {
    do {
        cout << "This is an infinite loop." << endl;
    } while (true);
    
    return 0;
}

Output

This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
........

Explanation: The loop executes at least once and continues indefinitely because the condition never becomes false.

Common Causes of Accidental Infinite Loops

Infinite loops are not always intentional. They often occur because of programming mistakes.

Missing Update Statements

A loop may never terminate if the variable controlling the loop condition is not updated.

C++
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i < 5) {
        cout << i <<endl;
        // Missing update: i++;
    }
    return 0;
}

Output

1
1
1
1
.......

Explanation: Since the loop variable never changes, the condition remains true forever.

Incorrect Loop Conditions

Using an incorrect termination condition can prevent a loop from ending.

C++
#include <iostream>
using namespace std;

int main() {
    int i = 2;
    while (i >= 0) {  // Should likely be i < some_value
        cout << "Hello Geeks " << endl;
        
    }
    return 0;
}

Output

Hello Geeks 
Hello Geeks 
Hello Geeks 
Hello Geeks 
Hello Geeks 
........

Explanation: If the condition is always true, the loop continues executing indefinitely.

Logical Erros in the Loop

Small logical mistakes can unintentionally create infinite loops.

C++
#include <iostream>
using namespace std;

int main() {
    for (int i = 3; i >2; i += 2) {  // i is always > 2
        cout <<"This is an infinite loop" << endl;
    }
    return 0;
}

Output

This is an infinite loop
This is an infinite loop
This is an infinite loop
This is an infinite loop
........

Explanation: The update expression and loop condition may work together in a way that prevents the termination condition from ever being reached.

Applications of Infinite Loops

Although infinite loops are often associated with programming errors, they also have several practical uses.

  • Event Loops: GUI applications use infinite loops to continuously respond to user actions.
  • Server Applications: Servers run continuously and repeatedly listen for client requests.
  • Embedded Systems: Microcontrollers often use infinite loops as their main execution cycle.
  • User Input Processing: Programs may repeatedly request input until a valid response is received.

Using Infinite Loops to Take User Input in C++

Infinite loops are frequently used when a program needs to repeatedly accept input until a specific condition is met.

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string input;
    
    while (true) {
        cout << "Enter a command (type 'exit' to quit): ";
        getline(cin, input);

        if (input == "exit") {
            break; // Exit the loop if the user types 'exit'
        }

        cout << "You entered: " << input << endl;
        // Process the input
    }
    cout << "Program exited." << endl;
    return 0;
}

Output

Enter a command (type 'exit' to quit): Hello
You entered: Hello
Enter a command (type 'exit' to quit): Geeks
You entered: Geeks
Enter a command (type 'exit' to quit): exit
Program exited.

Explanation: The loop continues accepting input from the user until the exit condition is satisfied. When the user enters the termination command, the loop ends using the break statement.

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment