Learning Objectives
- Write and trace WHILE loops.
- Explain why a WHILE loop may execute zero times.
- Design a condition and update that guarantee termination.
- Use WHILE for validation and sentinel-controlled repetition.
Key Terms
- Pre-condition loop
- A loop that tests its condition before executing the body.
- WHILE loop
- A pre-condition loop that repeats while a Boolean condition is TRUE.
- Sentinel value
- A special input value used to signal the end of data.
- Termination
- The point at which a loop stops.
- Infinite loop
- A loop whose condition never becomes false.

How WHILE Works
A WHILE loop tests its condition before every iteration. If the condition is false on the first test, the body is skipped completely. This is the defining feature of a pre-condition loop.
The body repeats only while the condition remains true. The loop must contain, or be affected by, a change that can eventually make the condition false.
WHILE is useful when the number of repetitions is not known in advance and it is possible that no repetition is needed.
Cambridge WHILE Syntax
The condition is Boolean. Statements belonging to the loop are indented. Execution returns to the condition after the body.
WHILE Condition DO
Statements
ENDWHILE
Validation With WHILE
The first input occurs before the loop because the condition needs a value to test. If the first mark is valid, the body is skipped. If invalid, the body repeatedly asks for a replacement.
The new INPUT inside the loop is essential. Without it, Mark would never change and the invalid condition would remain true forever.
OUTPUT "Enter a mark from 0 to 100"
INPUT Mark
WHILE Mark < 0 OR Mark > 100 DO
OUTPUT "Invalid. Enter again"
INPUT Mark
ENDWHILE
Sentinel-Controlled Input
A sentinel is a special value that ends input and is not processed as ordinary data. The first value is often input before the WHILE, then a new value is input at the end of each iteration.
For example, values may be totalled until -1 is entered. The condition WHILE Value <> -1 DO ensures the sentinel is not added to the total.
Choose a sentinel that cannot be confused with valid data. If all integers are valid, a separate Boolean or count may be needed instead.
Avoiding Infinite Loops
An infinite loop occurs when the condition remains true. Common causes are forgetting to update a counter, updating the wrong variable or writing the condition in the wrong direction.
Trace the condition and the changing variables. Ask what must eventually happen for the condition to become false. If no statement can cause that change, the loop cannot terminate normally.
WHILE Trace
| Before Test | Count < 3 | Body Action | Count After |
|---|---|---|---|
| 0 | TRUE | Output 0; add 1 | 1 |
| 1 | TRUE | Output 1; add 1 | 2 |
| 2 | TRUE | Output 2; add 1 | 3 |
| 3 | FALSE | Body skipped | 3 |
Worked Examples
Password Attempts
Question: Allow attempts while IsCorrect is FALSE and Attempts < 3.
- Initialise Attempts to zero and IsCorrect to FALSE.
- Use both conditions with AND.
- Input and test a password in the body.
- Increase Attempts each iteration.
Answer: WHILE NOT IsCorrect AND Attempts < 3 DO … ENDWHILE, with Attempts updated and IsCorrect set when a match occurs.
Countdown
Question: Write a WHILE loop that outputs 5 down to 1.
- Initialise Number to 5.
- Repeat while Number >= 1.
- Output Number.
- Subtract one.
Answer: Number <- 5; WHILE Number >= 1 DO OUTPUT Number; Number <- Number – 1; ENDWHILE.
Examination Guidance
- Explain that a WHILE body may execute zero times.
- Input or initialise data before the first condition test.
- Update condition variables inside the loop.
- Keep sentinel values out of normal processing.
- Use a condition that states when repetition should continue.
Common Mistakes
- Forgetting the first input before validation.
- Forgetting to update the loop variable.
- Using OR instead of AND in a limited-attempt condition.
- Adding the sentinel to a total.
- Writing a condition for stopping instead of continuing without reversing it.
Knowledge Check
1. When is a WHILE condition tested?
2. Can a WHILE loop execute zero times?
3. What is a sentinel?
4. What causes an infinite loop?
5. Why is input repeated inside a validation WHILE?