0
0
Cprogramming~10 mins

Do–while loop in C - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
C
int i = 1;
do {
    printf("%d ", i);
    i++;
} while (i <= 3);
This code prints numbers 1 to 3 using a do–while loop.
Execution Table
Stepi valueCondition (i <= 3)ActionOutput
11TruePrint 1, i = i + 11
22TruePrint 2, i = i + 12
33TruePrint 3, i = i + 13
44FalseExit loop
💡 i becomes 4, condition 4 <= 3 is False, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
i12344
Key Moments - 3 Insights
Why does the loop body run even when the condition is false at the start?
Because the do–while loop runs the body first before checking the condition, as shown in step 1 of the execution_table.
When does the condition get checked in a do–while loop?
The condition is checked after the loop body executes, as seen between steps 1 and 2 in the execution_table.
What happens when the condition becomes false?
The loop stops immediately after the condition check, as shown in step 4 where i=4 and condition is false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at step 3?
A4
B2
C3
D1
💡 Hint
Check the 'i value' column at step 3 in the execution_table.
At which step does the condition become false and the loop stops?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Condition' column and 'Action' column in the execution_table.
If the initial value of i was 5, how many times would the loop body run?
A1 time
B0 times
C5 times
DInfinite times
💡 Hint
Remember the do–while loop runs the body once before checking the condition.
Concept Snapshot
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
Full Transcript
A do–while loop in C runs the code inside the loop first, then checks the condition. If the condition is true, it repeats the loop. If false, it stops. This means the loop body always runs at least once, even if the condition is false at the start. For example, starting with i = 1, the loop prints i and increases it by 1 each time. It stops when i becomes 4 because the condition i <= 3 is false. This is different from a while loop that checks the condition before running the body. The key steps are: run the body, check the condition, repeat if true, exit if false.