Concept Flow - Do-while loop execution model
Start
Execute body
Check condition
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 { Console.WriteLine(i); i++; } while (i <= 3);
| Step | i value | Action | Condition (i <= 3) | Loop continues? | Output |
|---|---|---|---|---|---|
| 1 | 1 | Execute body: print i, increment i | 1 <= 3 is True | Yes | 1 |
| 2 | 2 | Execute body: print i, increment i | 2 <= 3 is True | Yes | 2 |
| 3 | 3 | Execute body: print i, increment i | 3 <= 3 is True | Yes | 3 |
| 4 | 4 | Condition checked, body skipped | 4 <= 3 is False | No |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| i | 1 | 2 | 3 | 4 | 4 |
do-while loop syntax:
do {
// code
} while (condition);
Runs loop body first, then checks condition.
Repeats if condition true, stops if false.
Always runs at least once.