do-while Loop
Run first, check laterA do-while loop is a while loop turned upside down: the body runs first and the condition is checked afterwards — so the body always runs at least once. That is exactly what menus and "ask again until the answer is valid" need. In the lab below you compare it with while on the same condition, run a menu, and validate input.
- do-whilechecks AFTER each round
- At least oncethe body always runs
- } while (…);ends with a semicolon
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
The shape of a do-while loop
do { body } while (condition); — the body runs first, then the condition is checked. True → the body runs again; false → the loop ends. Because the check comes after the body, the body always runs at least once. Note the semicolon after the closing parenthesis.
do {
statements; // runs FIRST
} while (condition); // checked AFTER — note the ;
// order: body → check → body → check → … → check (false) → done
| Loop | Condition checked | Fewest rounds | Typical use |
|---|---|---|---|
| for | before each round | 0 | counting a known number of times |
| while | before each round | 0 | repeat until something happens (sentinel) |
| do-while | after each round | 1 | menus, "ask again until valid" |
How do-while works
- do starts the loop — there is no condition at the top.
- The body runs.
- The condition in while ( … ); is checked AFTER the body.
- True → back to the top of the body; false → continue after the loop.
- The body always runs at least once, even if the condition is false from the start.
When to use it
- When the body MUST run once: show a menu, ask a question, roll a die.
- Input validation: do { ask; read; } while (input is bad);
- Menus: do { show; read choice; act; } while (choice != EXIT);
- Counting digits: a do-while also counts the single digit of 0.
- If the body may need to run zero times, use while instead.
Details
- The semicolon after while ( … ) is required.
- Use braces for the body — with several statements they are required.
- Variables used in the condition must be declared BEFORE do, not inside the body.
- Write the condition for the case when you want to go AGAIN.
- break and continue work in do-while too (next module).
Counting the digits of a number — even 0
Every line, explained.
#include <stdio.h>
int main(void)
{
int n = 0;
The tricky case: 0 has one digit.
int digits = 0;
do {
No check yet — the body runs at once.
digits++;
Count a digit: 1.
n /= 10;
Remove the last digit: 0 / 10 is 0.
} while (n != 0);
0 != 0 is false, so the loop ends after one round.
printf("%d digit(s)\n", digits);
Prints 1 digit(s). A while (n != 0) loop would never run and print 0!
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
A missing semicolon
do {
n++;
} while (n < 5)
do {
n++;
} while (n < 5);
do-while is the one loop that ends with a semicolon. Without it the compiler reports "expected ';'".
A condition the wrong way round
do {
scanf("%d", &n);
} while (n >= 1 && n <= 10);
do {
scanf("%d", &n);
} while (n < 1 || n > 10);
The condition says when to ask AGAIN — that is when the input is BAD.
Declaring the variable inside the body
do {
int n;
scanf("%d", &n);
} while (n < 0);
int n;
do {
scanf("%d", &n);
} while (n < 0);
n exists only inside the { } of the body, so the condition cannot see it: 'n' undeclared.
Using do-while when zero rounds are possible
int count = 0;
do {
avg = total / count;
} while (0);
if (count > 0)
avg = total / count;
do-while ALWAYS runs once — here that means dividing by 0. Use while or if when the body may need to be skipped.
Several statements without braces
do
printf("%d", n);
n++;
while (n < 5);
do {
printf("%d", n);
n++;
} while (n < 5);
Without braces only one statement can be the body, so the compiler finds n++ where it expects while: a syntax error.
Check your understanding
Answer each question — you will see the explanation straight away.
int n = 9; do { printf("%d ", n); n++; } while (n < 5); — output?
The body runs once (prints 9) before the false condition is checked.
Same code with while (n < 5) { … } instead of do-while — output?
while checks first: 9 < 5 is false, so the body never runs.
What is the minimum number of times a do-while body runs?
The check comes after the body, so at least once.
Which line correctly ends a do-while loop?
Parentheses around the condition and a semicolon at the end.
Ask for an age until it is between 1 and 120. Which condition goes in while ( … )?
Repeat while the age is invalid: below 1 OR above 120.
do-while Loop quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.