← IGCSE Computer Science Paper 2

WHILE ... DO ... ENDWHILE

A loop that tests its condition before each pass and repeats its statements while the condition is TRUE. It may run zero times, and the body must change the condition.

Understanding WHILE ... DO ... ENDWHILE in IGCSE Computer Science Paper 2

What it does: Number is 5, so Number > 9 is FALSE on the first test. The body is skipped and nothing is output. Number DIV 10 makes Number smaller each pass, so eventually Number > 9 becomes FALSE and the loop stops. A WHILE loop is a pre-condition loop. It checks the condition first, then runs the body only if the condition is TRUE. ENDWHILE closes a WHILE loop. NEXT closes a FOR loop and UNTIL ends a REPEAT loop. The body must change the variable tested in the condition, for example Number ← Number DIV 10.

Common mistakes: = is used for comparison, not for storing a value. Assignment must use the arrow, so the line should be Count ← Count + 1. A WHILE loop header ends with DO, not THEN. THEN belongs to IF statements. It should be WHILE Count < 5 DO.

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 166–175) and word search (Modules 166–187).

Take this worksheet further — free with Cloodit

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

Learning
Duo
When does a WHILE loop test its condition?
Before each pass through the loopAfter each pass through the loop
Matches
WHILE Number > 9 DO Count ← Count + 1 ENDWHILE
↓
Nothing in the body changes Number, so the condition stays TRUE and the loop never ends (an infinite loop)
Applying
Meanings
Term
WHILE ... DO ... ENDWHILE
Meaning
A loop that tests its condition before each pass and repeats its statements while the condition is TRUE. It may run zero times, and the body must change the condition.
Practice
Number ← 4725
Testing
Gaps
Number > 9 DO
Check Answer
Spots
2WHILE Number > 9 DO
3 Count = Count + 1

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

The 'Arctic Fox' badge — Tundra Tracker, 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 does a WHILE loop test its condition?
A. Before each pass through the loop ✓
B. After each pass through the loop
A WHILE loop is a pre-condition loop. It checks the condition first, then runs the body only if the condition is TRUE.
2. Number ← 5 WHILE Number > 9 DO Number ← Number + 1 ENDWHILE How many times does the body run?
A. At least once
B. Zero times ✓
Number > 9 is FALSE at the start, so the body is skipped. A WHILE loop may run zero times.
3. A WHILE loop repeats its statements while the condition is...
A. FALSE
B. TRUE ✓
WHILE keeps looping while the condition is TRUE and stops as soon as it becomes FALSE. (REPEAT ... UNTIL is the one that stops when its condition is TRUE.)
4. Which line correctly closes a WHILE loop?
A. ENDWHILE ✓
B. NEXT
ENDWHILE closes a WHILE loop. NEXT closes a FOR loop and UNTIL ends a REPEAT loop.
5. Number ← 20 WHILE Number > 9 DO OUTPUT Number ENDWHILE What is wrong with this loop?
A. Nothing in the body changes Number, so the loop never ends ✓
B. WHILE loops cannot use the > operator
The body must change something in the condition, such as Number ← Number - 1. Otherwise the condition stays TRUE forever.

Match the Pairs

WHILE Number > 9 DO → Tests the condition before each pass and runs the loop body only while Number is greater than 9
ENDWHILE → Closes the WHILE loop and sends control back to test the condition again
A WHILE loop tests its condition first, so if the condition starts FALSE the body runs zero times → Number ← 5 WHILE Number > 9 DO OUTPUT Number ENDWHILE
Something inside the body must change the condition, or the loop never ends → WHILE Number > 9 DO Number ← Number DIV 10 ENDWHILE
WHILE Number > 9 DO Count ← Count + 1 ENDWHILE → Nothing in the body changes Number, so the condition stays TRUE and the loop never ends (an infinite loop)
WHILE Number > 9 DO Number ← Number DIV 10 NEXT → The loop is closed with NEXT, which belongs to a FOR loop, instead of ENDWHILE

Key Term

WHILE ... DO ... ENDWHILE
A loop that tests its condition before each pass and repeats its statements while the condition is TRUE. It may run zero times, and the body must change the condition.

Fill in the Blanks

1. WHILE Number > 9 DO Number ← Number DIV 10 ENDWHILE
Hint: Type the keyword that starts a loop which tests its condition before each pass.
2. WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Hint: Type the keyword that ends the WHILE line and comes just before the loop body.
3. WHILE Number > 9 DO Number ← Number DIV 10 ENDWHILE
Hint: Type the keyword that closes the loop.
4. WHILE Count < 5 DO OUTPUT Count Count ← Count + 1
Hint: Type the variable the body must change so the condition eventually becomes FALSE.
5. WHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE
Hint: Type the relational operator so the loop repeats while Lives is larger than zero.

Spot the Error

Question 1
A Count ← 0
B WHILE Number > 9 DO
C Count = Count + 1
D Number ← Number DIV 10
E ENDWHILE
Line C — = is used for comparison, not for storing a value. Assignment must use the arrow, so the line should be Count ← Count + 1.
Question 2
A Count ← 0
B WHILE Count < 5 THEN
C OUTPUT Count
D Count ← Count + 1
E ENDWHILE
Line B — A WHILE loop header ends with DO, not THEN. THEN belongs to IF statements. It should be WHILE Count < 5 DO.
Question 3
A Number ← 100
B WHILE Number > 9 DO
C Number ← Number * 10
D ENDWHILE
E OUTPUT Number
Line C — The body must change the variable so the condition eventually becomes FALSE. Multiplying makes Number bigger, so Number > 9 stays TRUE forever. It should be Number ← Number DIV 10.
Question 4
A Count ← 0
B WHILE Count < 3 DO
C Count ← Count + 1
D ENDIF
E OUTPUT Count
Line D — A WHILE loop is closed with ENDWHILE. ENDIF closes an IF statement, so line 4 should be ENDWHILE.
Question 5
A Total ← 0
B WHILE Total < 50 DO
C INPUT Value
D Total ← Value
E ENDWHILE
F OUTPUT Total
Line D — This overwrites Total each pass instead of adding to it, so the total never builds up and the loop may never end. It should be Total ← Total + Value.

Student with a class code? Join your class →