Concept Flow - Repeat-while loop
Start
Execute loop body
Check condition
Exit loop
The repeat-while loop runs the code inside first, then checks the condition. If true, it repeats; if false, it stops.
var count = 1 repeat { print(count) count += 1 } while count <= 3
| Step | count value | Condition (count <= 3) | Action | Output |
|---|---|---|---|---|
| 1 | 1 | true | Print 1, count = 2 | 1 |
| 2 | 2 | true | Print 2, count = 3 | 2 |
| 3 | 3 | true | Print 3, count = 4 | 3 |
| 4 | 4 | false | Exit loop |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| count | 1 | 2 | 3 | 4 | 4 |
repeat {
// code to run
} while condition
- Runs loop body first, then checks condition
- Repeats while condition is true
- Always runs at least once
- Use when loop must run before condition check