Loop tracer Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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
Which loop should I use?
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

  1. do starts the loop — there is no condition at the top.
  2. The body runs.
  3. The condition in while ( … ); is checked AFTER the body.
  4. True → back to the top of the body; false → continue after the loop.
  5. The body always runs at least once, even if the condition is false from the start.

When to use it

  1. When the body MUST run once: show a menu, ask a question, roll a die.
  2. Input validation: do { ask; read; } while (input is bad);
  3. Menus: do { show; read choice; act; } while (choice != EXIT);
  4. Counting digits: a do-while also counts the single digit of 0.
  5. If the body may need to run zero times, use while instead.

Details

  1. The semicolon after while ( … ) is required.
  2. Use braces for the body — with several statements they are required.
  3. Variables used in the condition must be declared BEFORE do, not inside the body.
  4. Write the condition for the case when you want to go AGAIN.
  5. break and continue work in do-while too (next module).
Walkthrough

Counting the digits of a number — even 0

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int n = 0; The tricky case: 0 has one digit.
6 int digits = 0;
7 do { No check yet — the body runs at once.
8 digits++; Count a digit: 1.
9 n /= 10; Remove the last digit: 0 / 10 is 0.
10 } while (n != 0); 0 != 0 is false, so the loop ends after one round.
11 printf("%d digit(s)\n", digits); Prints 1 digit(s). A while (n != 0) loop would never run and print 0!
12 return 0;
13 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

A missing semicolon

✗ Wrong
do {
    n++;
} while (n < 5)
✓ Right
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

✗ Wrong
do {
    scanf("%d", &n);
} while (n >= 1 && n <= 10);
✓ Right
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

✗ Wrong
do {
    int n;
    scanf("%d", &n);
} while (n < 0);
✓ Right
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

✗ Wrong
int count = 0;
do {
    avg = total / count;
} while (0);
✓ Right
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

✗ Wrong
do
    printf("%d", n);
    n++;
while (n < 5);
✓ Right
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.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

int n = 9; do { printf("%d ", n); n++; } while (n < 5); — output?

Question 2

Same code with while (n < 5) { … } instead of do-while — output?

Question 3

What is the minimum number of times a do-while body runs?

Question 4

Which line correctly ends a do-while loop?

Question 5

Ask for an age until it is between 1 and 120. Which condition goes in while ( … )?

Assessment

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.