1. Concept and Definition
A do-while loop is a control flow statement that executes a block of code at least once and then repeats the execution as long as a specified condition is true.
Unlike the while loop, it guarantees that the code inside the loop will run at least once, even if the condition is initially false.
It is generally used in menu-driven programs, input validation, and situations where the block must execute first.
General Syntax:
Components:
do→ Keyword that marks the beginning of the loop block.{ }→ Curly braces containing one or more statements.while(condition);→ Checks the condition after execution. Must end with a semicolon.
2. Execution Flow – Step by Step
Enter the loop at the
dokeyword.Execute all statements inside the
{ }block.Check the condition in
while(condition).Repeat the block if the condition is true.
Exit the loop if the condition is false.
Flow Diagram (Text-Based)
3. Basic Example
Output:
Explanation:
Start with
i = 1.Execute
cout << i→ prints1.Increment
i→i = 2.Check
i <= 5→ true, repeat.Continue until
i = 6.i <= 5→ false, exit loop.
Observation: Loop executes 5 times.
4. Do-While with Initial False Condition
Output:
Explanation:
Condition
i <= 5is false from the start.Do block executes once anyway, prints
10.Condition checked → false → loop exits.
Key Point: This behavior distinguishes do-while from while loop.
5. Practical Applications of Do-While
Menu-Driven Programs:
Code executes at least once, showing the menu.
User decides whether to continue.
Input Validation:
Ensures user input is valid.
Loop repeats until a positive number is entered.
Repeated Calculations:
Allows user to perform repeated calculations.
Loop continues based on user choice.
6. Nested Do-While Loops
Do-while loops can be nested inside each other, like other loops.
Example:
Output:
Explanation:
Outer loop runs
i = 1to2.Inner loop runs
j = 1to3for eachi.Nested loops help perform multiple repetitive tasks in combination.
7. Common Mistakes in Do-While Loops
Forgetting the semicolon at the end:
Infinite loops:
Wrong condition logic:
Always double-check the condition logic.
\8. Difference Between While and Do-While
| Feature | While Loop | Do-While Loop |
|---|---|---|
| Execution | Checks condition first | Executes code first, then checks condition |
| Minimum Execution | May run 0 times | Runs at least once |
| Condition Check | Before loop body | After loop body |
| Use Case | When execution depends entirely on condition | When you want code to run at least once |
| Syntax Ending | No semicolon after closing brace | Must end with semicolon after while(condition); |
Example Comparison:
While Loop (Condition False Initially)
Output: (nothing)
Do-While Loop (Condition False Initially)
Output: 10
Observation: Do-while guarantees one execution, while while may not execute at all.
9. Real-Life Analogy
While loop: “I will start studying only if it is raining.” → If not raining → no study at all.
Do-while loop: “I will open the door and then check if it is raining.” → Door opens at least once, then decide.
Think of do-while as “execute first, check later”.
10. Step-by-Step Execution Table Example
Code:
| Step | i value | Print? | Condition i <= 3 | Next i |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 <= 3 → true | i=2 |
| 2 | 2 | 2 | 2 <= 3 → true | i=3 |
| 3 | 3 | 3 | 3 <= 3 → true | i=4 |
| 4 | 4 | – | 4 <= 3 → false | exit |