while Loop
Repeat while it is trueComputers are great at repeating things. A while loop runs its body again and again as long as a condition is true. In the lab below you watch every round — condition, body, update — while a trace table fills up row by row, read numbers until a stop value, and catch loops that never end.
- whilechecks the condition BEFORE each round
- Updatemoves the loop towards its end
- 0 roundsif the condition is false at the start
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 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
| 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
- The condition is checked BEFORE every round.
- True → the body runs, then back to the condition.
- False → the loop ends and the program continues after the }.
- If the condition is false at the start, the body runs 0 times.
- After the loop, the variable holds the value that made the condition false.
Writing a correct loop
- Initialise every variable the loop uses (counters, sums) before the loop.
- Make sure the update moves TOWARDS making the condition false.
- Prefer < or <= over != for stopping: a step can jump over the end value.
- Check the first and the last round (off-by-one errors).
- Never put a ; right after while ( … ).
Common loop patterns
- Counter: i = 1; while (i <= n) { …; i++; }
- Accumulator: sum = 0; … sum += x; inside the loop.
- Sentinel: read; while (value != stop) { use; read again; }
- Digits: while (n > 0) { d = n % 10; n /= 10; }
- An infinite loop is stopped with Ctrl+C in the terminal.
Counting and adding the digits of a number
Every line, explained.
#include <stdio.h>
int main(void)
{
int n = 4729;
int digits = 0, sum = 0;
A counter and an accumulator, both starting at 0.
while (n > 0) {
Repeat while digits are left: 4729, 472, 47, 4, then 0 stops it.
sum += n % 10;
n % 10 is the last digit: 9, 2, 7, 4.
n /= 10;
Integer division removes the last digit — this is the update.
digits++;
Count one more digit.
}
printf("%d digits, sum %d\n", digits, sum);
Prints 4 digits, sum 22
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
Forgetting the update
int i = 1;
while (i <= 5)
printf("%d\n", i);
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
while (i <= 5);
{
i++;
}
while (i <= 5)
{
i++;
}
The ; is the body. The block below is not in the loop, so it hangs without any output.
Stopping with !=
i = 1;
while (i != 10)
i += 2;
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
// print 1 to 10
i = 1;
while (i < 10) { … i++; }
// 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
int sum;
while (i <= 5) { sum += i; i++; }
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).
Check your understanding
Answer each question — you will see the explanation straight away.
int i = 1; while (i <= 3) { printf("%d ", i); i++; } — output?
i takes 1, 2, 3; when i becomes 4 the condition is false.
int i = 10; while (i < 5) { printf("x"); i++; } — how many x?
The condition is false at the start, so the body never runs.
What is i after: int i = 0; while (i < 4) i += 3;
0 → 3 (still < 4) → 6 (not < 4): the loop stops with i = 6.
What makes while (x > 0) printf("hi"); an infinite loop when x is 5?
The body never changes x, so x > 0 stays true.
When is the condition of a while loop checked?
while is a "pre-test" loop: it checks before running the body.
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.