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 while loop

while checks its condition; if it is true, the body runs, and then the condition is checked AGAIN. This repeats until the condition is false. Each run of the body is called an iteration (a round). If the condition is false at the very start, the body runs zero times.

int i = 1;              // 1. start value
while (i <= 5) {        // 2. condition — checked before every round
    printf("%d\n", i);   // 3. body — the work
    i++;                // 4. update — moves towards the end
}
// continues here when the condition is false
The four parts of every loop
Part In the example What it does If it is missing or wrong
Start value int i = 1; gives the loop variable its first value the loop starts from garbage
Condition i <= 5 decides whether another round runs too many or too few rounds
Body printf("%d\n", i); the work done in each round nothing useful happens
Update i++; changes the variable towards the end an infinite loop

How while works

  1. The condition is checked BEFORE every round.
  2. True → the body runs, then back to the condition.
  3. False → the loop ends and the program continues after the }.
  4. If the condition is false at the start, the body runs 0 times.
  5. After the loop, the variable holds the value that made the condition false.

Writing a correct loop

  1. Initialise every variable the loop uses (counters, sums) before the loop.
  2. Make sure the update moves TOWARDS making the condition false.
  3. Prefer < or <= over != for stopping: a step can jump over the end value.
  4. Check the first and the last round (off-by-one errors).
  5. Never put a ; right after while ( … ).

Common loop patterns

  1. Counter: i = 1; while (i <= n) { …; i++; }
  2. Accumulator: sum = 0; … sum += x; inside the loop.
  3. Sentinel: read; while (value != stop) { use; read again; }
  4. Digits: while (n > 0) { d = n % 10; n /= 10; }
  5. An infinite loop is stopped with Ctrl+C in the terminal.
Walkthrough

Counting and adding the digits of a number

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int n = 4729;
6 int digits = 0, sum = 0; A counter and an accumulator, both starting at 0.
7 while (n > 0) { Repeat while digits are left: 4729, 472, 47, 4, then 0 stops it.
8 sum += n % 10; n % 10 is the last digit: 9, 2, 7, 4.
9 n /= 10; Integer division removes the last digit — this is the update.
10 digits++; Count one more digit.
11 }
12 printf("%d digits, sum %d\n", digits, sum); Prints 4 digits, sum 22
13 return 0;
14 }
Common mistakes

Spot the bug

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

Forgetting the update

✗ Wrong
int i = 1;
while (i <= 5)
    printf("%d\n", i);
✓ Right
int i = 1;
while (i <= 5) {
    printf("%d\n", i);
    i++;
}

Without i++ the condition never becomes false: an infinite loop. Stop it with Ctrl+C.

A semicolon after while

✗ Wrong
while (i <= 5);
{
    i++;
}
✓ Right
while (i <= 5)
{
    i++;
}

The ; is the body. The block below is not in the loop, so it hangs without any output.

Stopping with !=

✗ Wrong
i = 1;
while (i != 10)
    i += 2;
✓ Right
i = 1;
while (i < 10)
    i += 2;

i is 1, 3, 5, 7, 9, 11 … and never exactly 10 — the loop never ends.

Off by one

✗ Wrong
// print 1 to 10
i = 1;
while (i < 10) { … i++; }
✓ Right
// print 1 to 10
i = 1;
while (i <= 10) { … i++; }

i < 10 stops after 9. Always check the first and last value.

An uninitialised total

✗ Wrong
int sum;
while (i <= 5) { sum += i; i++; }
✓ Right
int sum = 0;
while (i <= 5) { sum += i; i++; }

sum starts with garbage, so the total is wrong. Accumulators must start at 0 (or 1 for products).

Practice

Check your understanding

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

0 / 5 correct
Question 1

int i = 1; while (i <= 3) { printf("%d ", i); i++; } — output?

Question 2

int i = 10; while (i < 5) { printf("x"); i++; } — how many x?

Question 3

What is i after: int i = 0; while (i < 4) i += 3;

Question 4

What makes while (x > 0) printf("hi"); an infinite loop when x is 5?

Question 5

When is the condition of a while loop checked?

Assessment

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.