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 bodydo-while loop and a while loop in C?while loop checks the condition first, so it may not run the code block if the condition is false initially.do-while loopdo-while loop runs the code block once before checking the condition, ensuring at least one execution.do-while loop executes the code block at least once before checking the condition. -> Option Cdo-while loop in C?do-while loop requires braces around the code block and a semicolon after the while condition in parentheses.do { /* code */ } while (condition); with braces and semicolon.int i = 3;
do {
printf("%d ", i);
i--;
} while (i > 0);do-while loop code snippet:int x = 5;
do {
printf("%d", x);
x--;
} while x > 0;while condition must be enclosed in parentheses, like while (x > 0);.while x > 0; missing parentheses, causing a syntax error.do-while loop that asks the user to enter a positive number and repeats until the user enters a positive number. Which code snippet correctly implements this?Pass the test and this pattern moves unknown