Concept Flow - Do–while loop
Start
Execute body
Check condition
Yes
Execute body
No
Exit
The do–while loop runs the code inside the loop first, then checks the condition. If true, it repeats; if false, it stops.
int i = 1; do { printf("%d ", i); i++; } while (i <= 3);
| Step | i value | Condition (i <= 3) | Action | Output |
|---|---|---|---|---|
| 1 | 1 | True | Print 1, i = i + 1 | 1 |
| 2 | 2 | True | Print 2, i = i + 1 | 2 |
| 3 | 3 | True | Print 3, i = i + 1 | 3 |
| 4 | 4 | False | Exit loop |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| i | 1 | 2 | 3 | 4 | 4 |
do {
// code to run
} while (condition);
- Runs code block once before checking condition
- Repeats while condition is true
- Always runs at least once
- Condition checked after loop body