← IGCSE Computer Science Paper 2

WHILE versus REPEAT

WHILE tests first, so its body may never run, and it loops while the condition is TRUE. REPEAT tests after the body, so it runs at least once, and it stops when the condition is TRUE.

Understanding WHILE versus REPEAT in IGCSE Computer Science Paper 2

What it does: Both loops keep asking until Num is positive. WHILE uses Num <= 0 (keep going) and UNTIL uses Num > 0 (stop), so the conditions are opposites. Total < 5 is FALSE on the first test, so the WHILE loop skips its body and nothing is output. WHILE continues on TRUE. REPEAT stops on TRUE. This is why the condition must be reversed when you rewrite one as the other. The user must be asked at least once, so the test belongs after the body. REPEAT guarantees the body runs once before checking.

Common mistakes: REPEAT stops when the condition is TRUE. Count <= 3 is a WHILE-style 'keep going' condition, so the loop would stop after one pass. It should be UNTIL Count > 3. A WHILE loop must end with ENDWHILE. UNTIL belongs to a REPEAT loop. Line 4 should be ENDWHILE.

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
A student wants a loop whose body might never run at all. Which loop should they use?
WHILE ... DO ... ENDWHILEREPEAT ... UNTIL
Matches
REPEAT INPUT Password UNTIL Password <> "abc"
↓
REPEAT stops when the condition is TRUE, so this stops on a wrong password. The condition was not reversed: it should be UNTIL Password = "abc".
Applying
Meanings
Term
WHILE vs REPEAT
Meaning
WHILE tests first, so its body may never run, and it loops while the condition is TRUE. REPEAT tests after the body, so it runs at least once, and it stops when the condition is TRUE.
Practice
Count ← 1
Testing
Gaps
Check Answer
Spots
3 Count ← Count + 1
4UNTIL Count <= 3

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

The 'Fennec Fox' badge — Listener, 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. A student wants a loop whose body might never run at all. Which loop should they use?
A. REPEAT ... UNTIL
B. WHILE ... DO ... ENDWHILE ✓
WHILE tests the condition before the body, so if the condition is FALSE at the start the body is skipped. REPEAT always runs its body once before testing.
2. Which statement about REPEAT ... UNTIL is correct?
A. The body always runs at least once ✓
B. The body may run zero times
REPEAT tests its condition AFTER the body, so the body has already run once before the test. Zero iterations is only possible with WHILE.
3. Which describes when each loop keeps going?
A. WHILE loops while the condition is TRUE; REPEAT loops until the condition is TRUE ✓
B. WHILE stops when the condition is TRUE; REPEAT loops while the condition is TRUE
WHILE continues on TRUE. REPEAT stops on TRUE. This is why the condition must be reversed when you rewrite one as the other.
4. A loop must repeat until Number >= 1 (to keep asking for a number). Rewrite as WHILE: which condition is correct?
A. WHILE Number >= 1 DO
B. WHILE Number < 1 DO ✓
REPEAT ... UNTIL Number >= 1 stops when the condition is TRUE. WHILE continues when the condition is TRUE, so you need the opposite: Number < 1.
5. Number is 5 before this loop starts. How many times does the body run? WHILE Number < 3 DO OUTPUT Number Number ← Number + 1 ENDWHILE
A. 0 times ✓
B. 1 time
WHILE tests first. Number < 3 is FALSE straight away (5 is not less than 3), so the body is skipped. This is the zero-iteration case.

Match the Pairs

WHILE Count < 5 DO Count ← Count + 1 ENDWHILE → Tests the condition first and keeps looping while it is TRUE, so the body may run zero times
REPEAT Count ← Count + 1 UNTIL Count >= 5 → Runs the body first, then stops as soon as the condition is TRUE, so the body runs at least once
The same task written both ways needs the condition reversed: WHILE continues on TRUE, REPEAT stops on TRUE → WHILE Num <= 0 DO INPUT Num ENDWHILE REPEAT INPUT Num UNTIL Num > 0
Zero-iteration case: if the condition is already false at the start, a WHILE loop body never runs → Total ← 10 WHILE Total < 5 DO OUTPUT Total ENDWHILE
REPEAT INPUT Password UNTIL Password <> "abc" → REPEAT stops when the condition is TRUE, so this stops on a wrong password. The condition was not reversed: it should be UNTIL Password = "abc".
WHILE Count = 10 DO Count ← Count + 1 ENDWHILE → WHILE continues on TRUE, so with the condition copied from an UNTIL line it exits straight away or never runs. It should be WHILE Count < 10 DO.

Key Term

WHILE vs REPEAT
WHILE tests first, so its body may never run, and it loops while the condition is TRUE. REPEAT tests after the body, so it runs at least once, and it stops when the condition is TRUE.

Fill in the Blanks

1. WHILE Num < 10 DO Num ← Num + 1 ENDWHILE
Hint: Type the keyword that closes the loop which tests its condition before running the body.
2. REPEAT Num ← Num + 1 UNTIL Num >= 10
Hint: This REPEAT loop does the same job as a WHILE loop that runs while Num is less than 10. Type the operator that reverses that condition.
3. WHILE FALSE DO OUTPUT "Hi" ENDWHILE
Hint: Type the Boolean value that means the loop body never runs, even once.
4. REPEAT OUTPUT "Hi" UNTIL TRUE
Hint: Type the keyword that starts a loop which always runs its body at least once.
5. REPEAT INPUT Age UNTIL Age >= 0
Hint: Type the keyword that ends a loop which tests after the body and stops when the condition is TRUE.

Spot the Error

Question 1
A Count ← 1
B REPEAT
C Count ← Count + 1
D UNTIL Count <= 3
Line D — REPEAT stops when the condition is TRUE. Count <= 3 is a WHILE-style 'keep going' condition, so the loop would stop after one pass. It should be UNTIL Count > 3.
Question 2
A Number ← 1
B WHILE Number < 20 DO
C Number ← Number * 2
D UNTIL Number >= 20
Line D — A WHILE loop must end with ENDWHILE. UNTIL belongs to a REPEAT loop. Line 4 should be ENDWHILE.
Question 3
A Count ← 0
B WHILE Count < 5 DO
C Count ← Count + 1
D UNTIL Count = 5
Line B — This loop ends with UNTIL, so it must start with REPEAT. WHILE ... DO needs ENDWHILE at the end. Line 2 should be REPEAT.
Question 4
A Total ← 0
B WHILE Total < 10 DO
C Total ← Total - 2
D ENDWHILE
Line C — The WHILE loop continues while Total < 10. Subtracting 2 makes Total go down, so the condition stays TRUE forever. It should be Total ← Total + 2.
Question 5
A INPUT Guess
B WHILE Guess = 7 DO
C INPUT Guess
D ENDWHILE
Line B — WHILE keeps looping while the condition is TRUE. To keep asking until the guess is right, it must loop while Guess <> 7. With = the loop stops on a wrong guess and runs on a right one.

Student with a class code? Join your class →