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
// 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
#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
Tick 0
Tick 1
Tick 2
Loop stopped after 3 ticks6. Key Takeaways
- An infinite loop's condition never becomes false, so it repeats endlessly unless forced to stop.
for (;;)andwhile (true)are the standard intentional infinite-loop forms in C++.- Intentional infinite loops (server loops, game loops, event loops) exit via an internal
breakorreturn. - The most common unintentional cause is forgetting to update the variable used in the loop's condition.
Practice what you learned
1. Which of the following is a standard way to intentionally write an infinite loop in C++?
2. What is the most common cause of an unintentional infinite `while` loop?
3. How does a well-designed intentional infinite loop, like a server's request loop, typically stop?
4. What is the output of: `int i = 0; while (i < 3) { cout << i; }`?
5. Which condition expression is equivalent to `for (;;)` in terms of always being true?
Was this page helpful?
You May Also Like
while Loop in C++
Understand the C++ `while` loop, how its entry condition is tested before every iteration, and when to use it over a `for` loop.
for Loop in C++
Learn the C++ `for` loop syntax, how init/condition/increment control iteration, and when it is the best choice for a known number of repetitions.
break and continue in C++
Learn the difference between `break`, which exits a loop immediately, and `continue`, which skips to the next iteration, with clear C++ examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics