← IGCSE Computer Science Paper 2

WHILE…DO…ENDWHILE pre-condition loop

A pre-condition loop: the condition is tested before every repeat, so the body may run zero times. The loop keeps going while the condition is TRUE and is closed by ENDWHILE.

Understanding WHILE…DO…ENDWHILE pre-condition loop in IGCSE Computer Science Paper 2

What it does: Number starts at 0, so Number <> 0 is FALSE straight away and OUTPUT is skipped, because a WHILE loop tests before the body. WHILE...DO is closed by ENDWHILE. NEXT closes a FOR loop. Number stays 5, so Number <> 0 is always TRUE. The body needs a statement such as Number <- Number - 1 or INPUT Number. ENDWHILE marks the end of the loop body, after which the algorithm goes back to test the condition again. Add Count <- Count + 1 inside the loop so Count eventually passes 5 and the condition becomes FALSE.

Common mistakes: A WHILE loop header ends with DO, not THEN. THEN belongs to IF statements. It should be WHILE Number <> 0 DO. Count goes down, so Count <= 5 stays TRUE forever and the loop never ends. The body must move Count towards making the condition FALSE: Count <- Count + 1.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 379–388) and word search (Modules 379–398).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
When is the condition of a WHILE loop tested?
After each repeat of the bodyBefore each repeat of the body
Matches
WHILE Number <> 0 DO OUTPUT Number INPUT Number END
↓
The loop is closed with END instead of ENDWHILE, so the pseudocode is not valid
Applying
Meanings
Term
WHILE Number <> 0 DO
Meaning
A pre-condition loop: the condition is tested before every repeat, so the body may run zero times. The loop keeps going while the condition is TRUE and is closed by ENDWHILE.
Practice
INPUT Number
Testing
Gaps
Number <> 0 DO
Check Answer
Spots
2INPUT Number
3WHILE Number <> 0 THEN

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Arctic Hare' badge — Speed Indexer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. When is the condition of a WHILE loop tested?
A. Before each repeat of the body ✓
B. After each repeat of the body
WHILE is a pre-condition loop, so the condition is checked before the body runs, every time. REPEAT...UNTIL checks after.
2. Which keyword closes a WHILE loop in Cambridge pseudocode?
A. NEXT
B. ENDWHILE ✓
WHILE...DO is closed by ENDWHILE. NEXT closes a FOR loop.
3. Number <- 0 WHILE Number <> 0 DO OUTPUT Number ENDWHILE How many times does the body run?
A. Once
B. Zero times ✓
The condition Number <> 0 is FALSE at the start, so the body is skipped completely. A pre-condition loop can run zero times.
4. The condition in WHILE Number <> 0 DO is TRUE. What happens?
A. The loop keeps going ✓
B. The loop stops
A WHILE loop repeats while its condition is TRUE. It stops only when the condition becomes FALSE.
5. Which WHILE loop is written correctly?
A. WHILE Number <> 0 DO INPUT Number ENDWHILE ✓
B. WHILE Number <> 0 INPUT Number NEXT
A WHILE loop needs DO at the end of the first line and ENDWHILE to close it. NEXT belongs to FOR loops.

Match the Pairs

WHILE Number <> 0 DO → Start of a pre-condition loop: the condition is tested before each repeat, and the loop keeps going while Number is not 0
ENDWHILE → Closes the WHILE loop; the statements between WHILE and ENDWHILE are the body that repeats
If the condition is FALSE the first time it is tested, the loop body never runs (zero times) → Number <- 0 WHILE Number <> 0 DO OUTPUT Number ENDWHILE
The body must change a variable used in the condition so the loop can end → WHILE Number <> 0 DO INPUT Number ENDWHILE
WHILE Number <> 0 DO OUTPUT Number INPUT Number END → The loop is closed with END instead of ENDWHILE, so the pseudocode is not valid
Count <- 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE → Count never changes inside the body, so the condition stays TRUE forever and the loop never ends

Key Term

WHILE Number <> 0 DO
A pre-condition loop: the condition is tested before every repeat, so the body may run zero times. The loop keeps going while the condition is TRUE and is closed by ENDWHILE.

Fill in the Blanks

1. WHILE Number <> 0 DO INPUT Number ENDWHILE
Hint: Type the keyword that starts a pre-condition loop and tests the condition before each repeat.
2. WHILE Number <> 0 DO INPUT Number ENDWHILE
Hint: Type the keyword that closes the WHILE loop.
3. WHILE Number <> 0 DO INPUT Number ENDWHILE
Hint: Type the relational operator meaning not equal to, so the loop keeps going until the rogue value is entered.
4. WHILE Number <> 0 DO INPUT Number ENDWHILE
Hint: Type the keyword that follows the condition and begins the loop body.
5. WHILE Count < 5 DO Count <- Count + 1 ENDWHILE
Hint: Type the variable name that is tested in the condition, so the body changes it and the loop can end.

Spot the Error

Question 1
A Total <- 0
B INPUT Number
C WHILE Number <> 0 THEN
D Total <- Total + Number
E INPUT Number
F ENDWHILE
Line C — A WHILE loop header ends with DO, not THEN. THEN belongs to IF statements. It should be WHILE Number <> 0 DO.
Question 2
A Count <- 1
B WHILE Count <= 5 DO
C Count <- Count - 1
D OUTPUT "Hello"
E ENDWHILE
Line C — Count goes down, so Count <= 5 stays TRUE forever and the loop never ends. The body must move Count towards making the condition FALSE: Count <- Count + 1.
Question 3
A INPUT Number
B WHILE Number = 0 DO
C OUTPUT Number * 2
D INPUT Number
E ENDWHILE
Line B — The loop should keep going while Number is NOT the rogue value 0. With = 0 the condition is FALSE for most inputs, so the body is skipped. It should be WHILE Number <> 0 DO.
Question 4
A Number = 10
B WHILE Number > 0 DO
C OUTPUT Number
D Number <- Number - 3
E ENDWHILE
Line A — In Cambridge pseudocode, assignment uses the arrow <-. The = sign is a comparison. It should be Number <- 10.
Question 5
A Total <- 0
B WHILE Total > 100 DO
C INPUT Score
D Total <- Total + Score
E ENDWHILE
Line B — Total starts at 0, so Total > 100 is FALSE at the first test and the body never runs. The loop should continue while Total < 100.

Student with a class code? Join your class →