← IGCSE Computer Science Paper 2

WHILE…DO…ENDWHILE loop (7d)

A loop that tests the condition first and repeats its body while it is TRUE. The body may run zero times, and it must change the variable or the loop never ends.

Understanding WHILE…DO…ENDWHILE loop (7d) in IGCSE Computer Science Paper 2

What it does: Count starts at 10, so Count < 5 is FALSE on the first test. The body is skipped and nothing is output. Count ← Count + 1 changes the tested variable each time round. Eventually Count < 5 becomes FALSE and the loop ends. WHILE is a pre-condition loop. The condition is checked first. REPEAT...UNTIL is the loop that checks at the end. WHILE keeps looping as long as the condition is TRUE. It stops as soon as the condition becomes FALSE. Count stays 1, so Count <= 3 is always TRUE. Add Count ← Count + 1 inside the loop to fix it.

Common mistakes: A WHILE loop header must end with DO. It should be WHILE Count < 5 DO. A WHILE loop is closed with ENDWHILE. NEXT belongs to FOR loops.

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 265–274) and word search (Modules 254–274).

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?
After each run of the loop bodyBefore each run of the loop body, including the first
Matches
Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE
↓
Count is never changed inside the loop, so Count <= 5 stays TRUE forever and the loop is infinite. Add Count ← Count + 1 in the body.
Applying
Meanings
Term
WHILE condition DO
Meaning
A loop that tests the condition first and repeats its body while it is TRUE. The body may run zero times, and it must change the variable or the loop never ends.
Practice
Count ← 1
Testing
Gaps
Count < 5 DO
Check Answer
Spots
1WHILE Count < 5
2 OUTPUT Count

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

The 'Space Turtle' badge — Speed in Zero-G, 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 run of the loop body, including the first ✓
B. After each run of the loop body
WHILE is a pre-condition loop. The condition is checked first. REPEAT...UNTIL is the loop that checks at the end.
2. Num ← 10 WHILE Num < 5 DO OUTPUT Num ENDWHILE How many times does the loop body run?
A. 0 times ✓
B. 1 time
Num < 5 is FALSE at the first test, so the body is skipped. A WHILE loop body may run zero times.
3. A WHILE loop repeats its body while the condition is...
A. FALSE
B. TRUE ✓
WHILE keeps looping as long as the condition is TRUE. It stops as soon as the condition becomes FALSE.
4. Count ← 1 WHILE Count <= 3 DO OUTPUT Count ENDWHILE What is wrong with this loop?
A. OUTPUT cannot be used inside a WHILE loop
B. Count is never changed, so the loop never ends ✓
Count stays 1, so Count <= 3 is always TRUE. Add Count ← Count + 1 inside the loop to fix it.
5. Which line stops this loop from running forever? WHILE Total < 100 DO INPUT Score ... ENDWHILE
A. Total ← Total + Score ✓
B. OUTPUT Total
The variable in the condition (Total) must be updated inside the body so that the condition can eventually become FALSE. OUTPUT does not change it.

Match the Pairs

WHILE Count < 5 DO → Tests the condition before each repeat and runs the loop body only while Count < 5 is TRUE
ENDWHILE → Marks the end of the loop body, then the program jumps back to test the WHILE condition again
The body of a WHILE loop may run zero times because the condition is tested first → Count ← 10 WHILE Count < 5 DO OUTPUT Count ENDWHILE
The variable in the condition must be updated inside the loop so the condition can become FALSE → WHILE Count < 5 DO OUTPUT Count Count ← Count + 1 ENDWHILE
Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE → Count is never changed inside the loop, so Count <= 5 stays TRUE forever and the loop is infinite. Add Count ← Count + 1 in the body.
WHILE Count < 5 Count ← Count + 1 ENDWHILE → The keyword DO is missing from the WHILE line. It should be WHILE Count < 5 DO.

Key Term

WHILE condition DO
A loop that tests the condition first and repeats its body while it is TRUE. The body may run zero times, and it must change the variable or the loop never ends.

Fill in the Blanks

1. WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Hint: Type the keyword that starts a loop which tests its condition before each repeat.
2. WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Hint: Type the keyword that closes the WHILE loop.
3. WHILE Count < 3 DO Count ← Count + 1 ENDWHILE
Hint: Type the arithmetic operator that moves the counter up so the condition can eventually become FALSE.
4. WHILE Num > 0 DO Num ← Num - 1 ENDWHILE
Hint: Type the keyword that follows the condition and begins the loop body.
5. WHILE Num > 0 DO Num ← Num - 1 ENDWHILE
Hint: Type the variable being tested in the condition, which must be changed inside the loop to avoid an infinite loop.

Spot the Error

Question 1
A WHILE Count < 5
B OUTPUT Count
C Count ← Count + 1
D ENDWHILE
Line A — A WHILE loop header must end with DO. It should be WHILE Count < 5 DO.
Question 2
A Total ← 0
B WHILE Total < 100 DO
C Total ← Total + 10
D NEXT
E OUTPUT Total
Line D — A WHILE loop is closed with ENDWHILE. NEXT belongs to FOR loops.
Question 3
A Num ← 1
B WHILE Num <= 5 DO
C Num ← Num - 1
D OUTPUT Num
E ENDWHILE
Line C — Num goes down, so Num <= 5 is always TRUE and the loop never ends (infinite loop). The loop must add 1: Num ← Num + 1.
Question 4
A INPUT Guess
B WHILE Guess <> 7 DO
C OUTPUT Guess
D ENDWHILE
E OUTPUT "Correct"
Line C — The loop body must change Guess, otherwise the condition never becomes FALSE and the loop is infinite. It should be INPUT Guess.
Question 5
A Total ← 0
B Num ← 1
C WHILE Num <= 4 THEN
D Total ← Total + Num
E Num ← Num + 1
F ENDWHILE
Line C — WHILE loops use DO, not THEN. THEN is used with IF. It should be WHILE Num <= 4 DO.

Student with a class code? Join your class →