11.1 What is a WHILE Loop?
A WHILE loop is a pre-condition loop — the condition is tested first, and only if it is TRUE does the body execute. This means the body may run zero or more times: if the condition is FALSE at the very first check, the body is skipped entirely.
- 1Pre-condition: The condition is evaluated before each pass through the body, not after.
- 2May run zero times: If the condition is initially FALSE, the body is never executed — unlike REPEAT...UNTIL.
- 3Continue condition: The loop continues while the condition is TRUE and stops when it becomes FALSE (opposite of REPEAT...UNTIL).
- 4Manual update required: The loop control variable must be updated inside the body, otherwise the loop runs forever.
Key Idea
Think of WHILE as "as long as this is true, keep doing this." The check happens at the start of each iteration — if it fails immediately, the loop never begins.
Common Use
WHILE is ideal for sentinel-controlled reading (e.g. read values until a special stop value) and state-driven loops where the loop should not run if the initial state already satisfies the exit condition.
Concepts — What a WHILE Loop Means
11.2 Syntax
The CIE pseudocode syntax for a WHILE loop uses three keywords:WHILEto start (with the condition), DOto mark the start of the body, andENDWHILEto close the loop.
WHILE <condition> DO
<statements to repeat>
ENDWHILEDECLARE Counter : INTEGER
Counter <- 1
WHILE Counter <= 5 DO
OUTPUT Counter
Counter <- Counter + 1
ENDWHILEWHILE
Starts the loop. The condition follows on the same line.
DO
Marks the beginning of the loop body. Comes after the condition.
ENDWHILE
Closes the loop. No condition is written here — control jumps back to WHILE.
Watch Out
Every WHILE must have a matching ENDWHILE. And the DO keyword is required in CIE pseudocode — forgetting it is a common syntax error. Indent the body to make the structure clear.
The condition can be any expression that produces a BOOLEAN result, such as Counter <= 5, Value <> -1, or (Total < Limit) AND (More = TRUE).
Syntax — Writing WHILE Loops Correctly
11.3 Tracing
A trace table for a WHILE loop records the value of the loop control variable and the condition before each iteration. If the condition is FALSE at the start, the body is skipped and no rows are added.
DECLARE Counter : INTEGER
DECLARE Total : INTEGER
Counter <- 1
Total <- 0
WHILE Counter <= 4 DO
Total <- Total + Counter
Counter <- Counter + 1
ENDWHILE
OUTPUT TotalTrace table:
| Iteration | Counter (start) | Counter <= 4? | Total (after) | Counter (after) |
|---|---|---|---|---|
| 1 | 1 | TRUE — run body | 1 | 2 |
| 2 | 2 | TRUE — run body | 3 | 3 |
| 3 | 3 | TRUE — run body | 6 | 4 |
| 4 | 4 | TRUE — run body | 10 | 5 |
| — | 5 | FALSE — exit loop | — | — |
The loop exits when Counter becomes 5, because 5 <= 4 is FALSE. The final value of Total is 10(1 + 2 + 3 + 4), which is what the OUTPUT prints.
Tracing Rule
In a WHILE loop, the condition is tested before each iteration. If it starts FALSE, the body is skipped — record "FALSE — exit" as the first (and only) row.
DECLARE X : INTEGER
X <- 100
WHILE X < 10 DO
OUTPUT "Inside loop"
X <- X + 1
ENDWHILE
OUTPUT "Done"Here X starts at 100. The condition X < 10 is FALSE on the very first check, so "Inside loop" is neverprinted. The program jumps straight to printing "Done".
Tracing — Step Through the Loop
11.4 WHILE vs REPEAT vs FOR
CIE pseudocode has three loop constructs. Choosing the right one is a common exam skill. The decision rests on two questions: do you know how many times to iterate in advance? and must the body run at least once?
| Feature | FOR | WHILE | REPEAT...UNTIL |
|---|---|---|---|
| Iteration count known in advance? | Yes (definite) | No (indefinite) | No (indefinite) |
| Condition checked | Auto (counter) | Before body | After body |
| Body runs at least once | Yes (if range valid) | No (may skip) | Yes (always) |
| Loop continues when condition is | N/A | TRUE | FALSE |
| Loop exits when condition is | N/A | FALSE | TRUE |
| Closing keyword | NEXT | ENDWHILE | UNTIL (with condition) |
| Best for | Counted iteration | Sentinel/state loops | Input validation, menus |
// FOR — count is known
FOR Counter <- 1 TO 5
OUTPUT Counter
NEXT Counter
// WHILE — pre-condition
DECLARE Counter : INTEGER
Counter <- 1
WHILE Counter <= 5 DO
OUTPUT Counter
Counter <- Counter + 1
ENDWHILE
// REPEAT...UNTIL — post-condition
DECLARE Counter : INTEGER
Counter <- 1
REPEAT
OUTPUT Counter
Counter <- Counter + 1
UNTIL Counter > 5Decision Guide
- Know the count? →
FOR - May not run at all? →
WHILE - Must run at least once? →
REPEAT...UNTIL
Exam Tip
A question that says "read numbers until -1 is entered" could use either WHILE or REPEAT — but if the very first input might be -1 and the body should not process it, WHILE is safer. A question that says "the user must be prompted at least once" signals REPEAT...UNTIL.
WHILE vs REPEAT vs FOR — Pick the Right Loop
11.5 Practical Examples
WHILE is most often used for sentinel-controlled reading, state-driven loops, and bounded accumulation. Let's look at a worked example of each.
Example 1 — Sentinel-Controlled Reading
DECLARE Value : INTEGER
DECLARE Total : INTEGER
Total <- 0
OUTPUT "Enter a value (0 to stop): "
INPUT Value
WHILE Value <> 0 DO
Total <- Total + Value
OUTPUT "Enter a value (0 to stop): "
INPUT Value
ENDWHILE
OUTPUT "Total is ", TotalThe first INPUT is read before the WHILE so the condition has something to test. Inside the body, each new INPUT updates Value. When the user enters 0, the loop exits — and 0 is not added to Total.
Example 2 — Bounded Accumulation
DECLARE N : INTEGER
DECLARE Sum : INTEGER
N <- 1
Sum <- 0
WHILE Sum + N < 100 DO
Sum <- Sum + N
N <- N + 1
ENDWHILE
OUTPUT "Final sum is ", Sum
OUTPUT "Stopped at N = ", NHere the loop checks whether adding the next value would stay under 100. If not, it exits before the addition — Sum never overshoots.
Example 3 — State-Driven Loop
DECLARE KeepGoing : BOOLEAN
DECLARE Choice : STRING
KeepGoing <- TRUE
WHILE KeepGoing = TRUE DO
OUTPUT "Playing a turn..."
OUTPUT "Type QUIT to stop, anything else to continue: "
INPUT Choice
IF Choice = "QUIT" THEN
KeepGoing <- FALSE
ENDIF
ENDWHILE
OUTPUT "Game over"A boolean flag (KeepGoing) controls the loop. The flag is initialised to TRUE before the loop, and flipped to FALSE inside the body when the user types QUIT.
Initialise First
Because WHILE tests the condition first, you must initialise the loop control variable before the loop. Forgetting to do this leaves the variable undefined and the condition unpredictable.
Practical Examples — Apply What You Know
11.6 Key Points Summary
Here is a quick recap of everything we've covered on WHILE loops:
- Pre-condition loop — The condition is checked BEFORE the body executes.
- May run zero times — If the condition is initially FALSE, the body is skipped entirely.
- Continue on TRUE — The loop keeps iterating while the condition is TRUE; it stops when FALSE.
- Three keywords — WHILE <condition> DO ... ENDWHILE — all three are required in CIE pseudocode.
- Initialise first — The loop control variable must be given a value BEFORE the WHILE statement.
- Manual update — You must update the loop control variable inside the body to avoid infinite loops.
- Choose by intent — Use FOR for known counts, WHILE for sentinel/state, REPEAT for at-least-once prompts.
Exam Tip
When a question says "while X is true, keep doing Y" or "keep reading until the sentinel appears (but the sentinel might be first)" — reach for WHILE. Always initialise the control variable before the loop, and remember to update it inside the body.