100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Infinite Loops in C++

Understand how infinite loops form in C++, how to write them intentionally with `for(;;)` or `while(true)`, and how to avoid the common bugs that cause them.

LoopsBeginner6 min readJul 7, 2026
Analogies

1. Intro

An infinite loop is a loop whose terminating condition never becomes false, so it keeps repeating forever (or until the program is forcibly stopped). Infinite loops can be a bug caused by forgetting to update a counter, but they are also written intentionally in real systems — such as server request-handling loops, game loops, and embedded device event loops — which run continuously and only exit via an internal break, return, or external signal.

🏏

Cricket analogy: A T20 super over with no maximum-overs rule would replay forever like a bowler bowling wide after wide because no umpire calls 'over complete' — an infinite loop is a condition, like 'over finished', that never triggers.

2. Syntax

cpp
// common intentional infinite loop forms
for (;;) {
    // runs forever until break/return
}

while (true) {
    // runs forever until break/return
}

3. Explanation

for (;;) omits all three clauses, so C++ treats the missing condition as always true. while (true) is more explicit about the same idea. Both loop forever unless something inside the body forcibly exits — typically a break statement, a return from the enclosing function, or a call like exit(). This pattern is common for programs that must keep working continuously, such as a server accepting connections in a loop, or a game's main update-and-render loop, where the exit condition (like 'shutdown requested') is checked inside the body and triggers a break. The far more common case, however, is an *unintentional* infinite loop: a bug where the programmer forgets to update the loop variable, e.g. int i = 0; while (i < 10) { cout << i; } — because i never changes, i < 10 is true forever.

🏏

Cricket analogy: A commentator's 'live and continuous' rain-delay broadcast, like for(;;), keeps running with no built-in end, cutting away only when the ground staff signal 'play resumes' — the equivalent of a break.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    int count = 0;
    for (;;) {
        cout << "Tick " << count << endl;
        count++;
        if (count == 3) {
            break; // intentional controlled exit
        }
    }
    cout << "Loop stopped after " << count << " ticks" << endl;
    return 0;
}

5. Output

text
Tick 0
Tick 1
Tick 2
Loop stopped after 3 ticks

6. Key Takeaways

  • An infinite loop's condition never becomes false, so it repeats endlessly unless forced to stop.
  • for (;;) and while (true) are the standard intentional infinite-loop forms in C++.
  • Intentional infinite loops (server loops, game loops, event loops) exit via an internal break or return.
  • The most common unintentional cause is forgetting to update the variable used in the loop's condition.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#InfiniteLoopsInC#Infinite#Loops#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep