1. Intro
The while loop is C++'s simplest looping construct: it repeats a block of code as long as a given condition remains true. Unlike the for loop, it does not bundle initialization or update logic into its header, making it the natural choice when the number of iterations is not known ahead of time — such as reading input until a sentinel value appears.
Cricket analogy: A while loop bowling 'until the batsman is out' mirrors a bowler continuing to bowl overs without a fixed pre-set count - unlike a for loop's fixed 20-over T20 limit, the innings continues until a specific condition (a wicket) ends it.
2. Syntax
while (condition) {
// loop body
}3. Explanation
Before every iteration, the while loop evaluates condition. If it is true, the body executes and then control jumps back to re-check the condition. If it is false, the loop terminates and execution continues after the closing brace. Because the check happens *before* the body runs, a while loop is an entry-controlled loop and can execute zero times if the condition is false from the start. You must ensure something inside the body eventually makes the condition false (such as incrementing a counter or reading new input) — otherwise the loop becomes infinite.
Cricket analogy: A while loop is entry-controlled like a bowler checking with the umpire before every single delivery whether overs remain - if the quota's already used, that over never even starts, and someone must actually bowl (update the counter) or the innings never ends.
4. Example
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << "Value: " << i << endl;
i++;
}
return 0;
}5. Output
Value: 1
Value: 2
Value: 3
Value: 4
Value: 56. Key Takeaways
- A
whileloop checks itsconditionbefore each iteration, making it entry-controlled. - The loop body can execute zero times if the condition starts out false.
- The counter or state variable must be declared and updated manually, unlike in a
forloop. - Best suited for loops with an unknown or condition-driven number of iterations.
Practice what you learned
1. When is the condition of a `while` loop evaluated?
2. What is the output of: `int i = 5; while (i < 5) { cout << i; i++; }`
3. Which statement about `while` loops is TRUE?
4. What is most likely to cause an infinite `while` loop?
5. How many times does `int i = 0; while (i < 3) { i++; }` execute the body?
Was this page helpful?
You May Also Like
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.
do-while Loop in C++
Learn how the C++ `do-while` loop guarantees at least one execution by checking its condition after the body runs, with a full iteration trace.
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.
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