← IGCSE Computer Science Paper 2

WHILE ... DO ... ENDWHILE loop (7a)

A loop that tests its condition first, then repeats its statements while the condition is TRUE. If it is FALSE at the start, the loop never runs. ENDWHILE marks the end of the loop.

Understanding WHILE ... DO ... ENDWHILE loop (7a) in IGCSE Computer Science Paper 2

What it does: Number is 10, so Number < 5 is FALSE at the start. The body is skipped and nothing is output. Count increases by 1 each pass. Once Count reaches 5 the condition is FALSE and the loop stops. A WHILE loop is a pre-condition loop. It checks the condition first, so it may never run. A REPEAT UNTIL loop checks at the end. Num is 1 and the condition is TRUE, so the loop starts. Nothing inside changes Num, so it is an infinite loop. Add Num ← Num + 1 inside the loop. Each loop type has its own ending keyword. NEXT closes a FOR loop, while a WHILE loop must be closed with ENDWHILE.

Common mistakes: A WHILE condition must compare values. ← is assignment, not a comparison. It should be WHILE Count <= 5 DO. Cambridge pseudocode keywords are written in capital letters. It should be WHILE Lives > 0 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 37–46) and word search (Modules 27–46).

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 the statements inside the loop runAfter the statements inside the loop run
Matches
Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE
↓
Count is never changed inside the loop, so the condition stays TRUE forever and the loop never ends. Add Count ← Count + 1 before ENDWHILE.
Applying
Meanings
Term
WHILE condition DO ... ENDWHILE
Meaning
A loop that tests its condition first, then repeats its statements while the condition is TRUE. If it is FALSE at the start, the loop never runs. ENDWHILE marks the end of the loop.
Practice
Count ← 1
Testing
Gaps
Count < 5 DO
Check Answer
Spots
1Count ← 1
2WHILE Count ← 5 DO

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

The 'Star Fox' badge — Frozen Master, 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. After the statements inside the loop run
B. Before the statements inside the loop run ✓
A WHILE loop is a pre-condition loop. It checks the condition first, so it may never run. A REPEAT UNTIL loop checks at the end.
2. Count ← 10 WHILE Count < 5 DO OUTPUT Count ENDWHILE What happens when this runs?
A. The loop body runs once
B. The loop body never runs ✓
Count < 5 is FALSE at the start, so the loop is skipped. This is the key difference from REPEAT ... UNTIL, which always runs at least once.
3. A WHILE loop repeats its statements for as long as the condition is...
A. TRUE ✓
B. FALSE
WHILE keeps looping while the condition is TRUE and stops as soon as it becomes FALSE. REPEAT UNTIL is the opposite: it stops when the condition is TRUE.
4. Which keyword marks the end of a WHILE loop?
A. ENDWHILE ✓
B. NEXT
WHILE ... DO ... ENDWHILE. NEXT ends a FOR loop, and UNTIL ends a REPEAT loop.
5. Num ← 1 WHILE Num <= 5 DO OUTPUT Num ENDWHILE What is wrong with this loop?
A. Num never changes, so the loop never ends ✓
B. The loop will never start
Num is 1 and the condition is TRUE, so the loop starts. Nothing inside changes Num, so it is an infinite loop. Add Num ← Num + 1 inside the loop.

Match the Pairs

WHILE Count <= 5 DO → Tests the condition first and repeats the loop body only while Count is less than or equal to 5
ENDWHILE → Marks the end of the statements that belong to the WHILE loop and sends control back to test the condition again
A WHILE loop tests its condition before the body, so if the condition is FALSE at the start the loop never runs → Number ← 10 WHILE Number < 5 DO OUTPUT Number ENDWHILE
A variable in the condition must be changed inside the loop body so the condition eventually becomes FALSE and the loop ends → WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Count ← 1 WHILE Count <= 5 DO OUTPUT Count ENDWHILE → Count is never changed inside the loop, so the condition stays TRUE forever and the loop never ends. Add Count ← Count + 1 before ENDWHILE.
WHILE Count <= 5 DO Count ← Count + 1 NEXT → The WHILE loop is closed with NEXT, which belongs to FOR loops. It must end with ENDWHILE.

Key Term

WHILE condition DO ... ENDWHILE
A loop that tests its condition first, then repeats its statements while the condition is TRUE. If it is FALSE at the start, the loop never runs. ENDWHILE marks the end of the loop.

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 running.
2. WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Hint: Type the keyword that marks the end of the loop.
3. WHILE Total < 100 DO Total ← Total + 10 ENDWHILE
Hint: Type the keyword that comes after the condition and before the loop statements.
4. WHILE Count < 5 DO Count ← Count + 1 ENDWHILE
Hint: Type the arithmetic operator that changes the variable so the condition eventually becomes FALSE.
5. WHILE Password <> "abc" DO INPUT Password ENDWHILE
Hint: Type the relational operator meaning not equal to, so the loop repeats until the password matches.

Spot the Error

Question 1
A Count ← 1
B WHILE Count ← 5 DO
C OUTPUT Count
D Count ← Count + 1
E ENDWHILE
Line B — A WHILE condition must compare values. ← is assignment, not a comparison. It should be WHILE Count <= 5 DO.
Question 2
A While Lives > 0 DO
B OUTPUT "Keep going"
C Lives ← Lives - 1
D ENDWHILE
Line A — Cambridge pseudocode keywords are written in capital letters. It should be WHILE Lives > 0 DO.
Question 3
A Num ← 10
B WHILE Num > 0 DO
C Num ← Num + 2
D OUTPUT Num
E ENDWHILE
Line C — Num goes up, so it moves away from 0 and the condition Num > 0 never becomes FALSE. The loop never ends. It should be Num ← Num - 2.
Question 4
A Score ← 0
B WHILE Score > 10 DO
C Score ← Score + 2
D OUTPUT Score
E ENDWHILE
Line B — The condition is tested first. Score starts at 0, so Score > 10 is FALSE straight away and the loop never runs. It should be WHILE Score < 10 DO.
Question 5
A Count = 1
B WHILE Count <= 4 DO
C OUTPUT Count * 2
D Count ← Count + 1
E ENDWHILE
Line A — Giving a variable a starting value needs the assignment arrow. = is used to compare values. It should be Count ← 1.

Student with a class code? Join your class →