1. Intro
The do-while loop is an exit-controlled loop: it runs the loop body first and only afterward checks whether it should repeat. This single distinguishing fact — the condition is evaluated AFTER the body — means a do-while loop always executes its body at least once, no matter what the condition is. This is the most important thing to remember about do-while, and it is a common source of exam trick questions.
Cricket analogy: A do-while loop is like a bowler who must bowl at least one over before the umpire even checks whether the innings should continue — the ball is bowled first, and only then is the over-limit condition checked, unlike a pre-checked declaration.
2. Syntax
do {
// loop body
} while (condition);3. Explanation
Execution starts with the do block: the body runs unconditionally the first time. Only after the body finishes does C++ evaluate condition. If it is true, control jumps back to the top of the body and runs it again; if false, the loop ends. Note the required semicolon after while (condition); — omitting it is a compile error. Because the check comes last, do-while is ideal for menu-driven programs or input validation where you must show a prompt or process something at least once before deciding whether to repeat.
Cricket analogy: Execution starts with the umpire signaling play (do block) unconditionally; only after the over finishes does the umpire check the innings condition — true means bowl another over, false ends it, and forgetting the closing semicolon after while() is like an incomplete declaration; this pattern suits toss-and-play formats where you must bowl at least once before deciding to continue.
4. Example
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << "i = " << i << endl;
i++;
} while (i <= 3);
// Demonstrates body runs once even if condition starts false
int n = 10;
do {
cout << "n was " << n << ", body still ran once" << endl;
} while (n < 5);
return 0;
}5. Output
i = 1
i = 2
i = 3
n was 10, body still ran once6. Key Takeaways
do-whilechecks its condition AFTER the body, so the body always runs at least once.- The
while (condition);line must end with a semicolon. - Use
do-whilewhen the body must run before it makes sense to test the condition, e.g. menus or input prompts. - Trace carefully: with
int i = 1; do {...i++;} while (i <= 3);the body runs for i = 1, 2, 3 (three times) before stopping.
Practice what you learned
1. What is the key difference between `while` and `do-while` loops?
2. How many times does the body run in: `int x = 100; do { cout << x; } while (x < 10);`?
3. Which syntax element is REQUIRED at the end of a `do-while` statement but not a `while` statement?
4. Trace: `int i = 0; do { i += 2; cout << i << " "; } while (i < 6);` — what is printed?
5. `do-while` is most appropriate for which scenario?
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.
if-else Statement in C++
Understand how the C++ `if-else` statement provides a two-way branch, executing one block when the condition is true and another when false.
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