← IGCSE Computer Science Paper 2

Tracing WHILE and REPEAT loops

Trace a loop by testing its condition each pass: WHILE tests before the body and may never run; REPEAT tests after the body so runs once at least. Stop when the loop ends.

Understanding Tracing WHILE and REPEAT loops in IGCSE Computer Science Paper 2

What it does: Each pass of the body ends with a new INPUT, so the trace table takes the next test value each time. The WHILE condition is then tested with that new value. Count is never changed inside the loop, so Count < 5 is always TRUE. Tracing shows the same row repeating, which reveals an infinite loop. REPEAT tests at the bottom at UNTIL. WHILE is the loop that tests at the top.

Common mistakes: A WHILE loop header needs the keyword DO after the condition. It should be WHILE Count <= 3 DO. Total goes 0, -4, -8... and never reaches 10, so the condition Total < 10 is always true and the loop never ends. It should be Total ← Total + 4.

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 83–92) and word search (Modules 71–92).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A trace of WHILE Count < 3 DO starts with Count = 5. How many times does the loop body run?
1 time, because the body always runs once0 times, because the condition is tested first and is FALSE
Matches
Trace of: X ← 8 WHILE X < 5 DO X ← X + 1 ENDWHILE Trace shows X = 9 after the loop
↓
The student ran the body without testing first. X < 5 is false at the start (8 is not less than 5), so the body never runs and X stays 8.
Applying
Meanings
Term
WHILE and REPEAT loop trace
Meaning
Trace a loop by testing its condition each pass: WHILE tests before the body and may never run; REPEAT tests after the body so runs once at least. Stop when the loop ends.
Practice
Total ← 0
Testing
Gaps
Check Answer
Spots
1Count ← 1
2WHILE Count <= 3

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

The 'Peregrine Falcon' badge — Operator Ace, 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 trace of WHILE Count < 3 DO starts with Count = 5. How many times does the loop body run?
A. 0 times, because the condition is tested first and is FALSE ✓
B. 1 time, because the body always runs once
WHILE tests its condition before the body, so if it is FALSE at the start the body is skipped completely.
2. A REPEAT loop ends with UNTIL Num > 0. The first test input is 7. How many times does the body run?
A. Once, then the loop stops ✓
B. 0 times, because 7 already passes the test
REPEAT tests after the body, so the body runs once before the condition is checked. Num > 0 is TRUE, so the loop ends.
3. When tracing a REPEAT ... UNTIL loop, where do you check the condition on each pass?
A. After the body has run ✓
B. Before the body runs
REPEAT tests at the bottom at UNTIL. WHILE is the loop that tests at the top.
4. REPEAT INPUT Num UNTIL Num >= 0 is traced with test inputs -3, -1, 6. How many values are read?
A. 1, because the loop stops after the first input
B. 3, because the loop stops only when 6 is entered ✓
Use the next test input each pass. -3 and -1 make Num >= 0 FALSE, so the loop repeats. 6 makes it TRUE, so it stops.
5. Trace WHILE Total < 10 DO Total ← Total + 4 ENDWHILE with Total starting at 0. What is Total when the loop ends?
A. 10
B. 12 ✓
Total goes 0, 4, 8, 12. At 8 the condition is TRUE, so the body runs once more. At 12 it is FALSE and the loop ends.

Match the Pairs

WHILE Total < 10 DO Total ← Total + 4 ENDWHILE → The condition Total < 10 is tested BEFORE each pass of the body, so the body may run zero times.
REPEAT INPUT Guess UNTIL Guess = 7 → The body runs first and the condition Guess = 7 is tested AFTER it, so the loop runs at least once and stops when the condition is true.
When tracing a loop that reads input, use the next test input on each pass and stop when the sentinel value is read. → INPUT Num WHILE Num <> -1 DO Total ← Total + Num INPUT Num ENDWHILE
A loop whose condition never changes to end it will never stop, so the trace table keeps adding rows forever. → Count ← 1 WHILE Count < 5 DO OUTPUT Count ENDWHILE
Trace of: X ← 8 WHILE X < 5 DO X ← X + 1 ENDWHILE Trace shows X = 9 after the loop → The student ran the body without testing first. X < 5 is false at the start (8 is not less than 5), so the body never runs and X stays 8.
Trace of: N ← 10 REPEAT N ← N + 1 UNTIL N > 5 Trace shows the loop does not run → REPEAT tests after the body, so the body always runs once. N becomes 11, and then N > 5 is true, so the loop stops after one pass.

Key Term

WHILE and REPEAT loop trace
Trace a loop by testing its condition each pass: WHILE tests before the body and may never run; REPEAT tests after the body so runs once at least. Stop when the loop ends.

Fill in the Blanks

1. WHILE Count < 3 DO Count ← Count + 1 ENDWHILE
Hint: Type the keyword that closes a loop which tests its condition before the body.
2. REPEAT Count ← Count + 1 UNTIL Count = 3
Hint: Type the keyword that tests the condition after the body, so the body runs at least once.
3. Count ← 5 WHILE Count < 3 DO OUTPUT Count
Hint: Type the loop keyword that tests first, so the body may never run when the condition starts false.
4. REPEAT INPUT Value UNTIL Value = 0
Hint: Type the variable that stores each new test input so the UNTIL test uses it on every pass.
5. WHILE Count < 3 DO Count ← Count + 1 ENDWHILE
Hint: Type the variable that must change in the body so the condition eventually becomes false and the loop does not run forever.

Spot the Error

Question 1
A Count ← 1
B WHILE Count <= 3
C OUTPUT Count
D Count ← Count + 1
E ENDWHILE
Line B — A WHILE loop header needs the keyword DO after the condition. It should be WHILE Count <= 3 DO.
Question 2
A Total ← 0
B WHILE Total < 10 DO
C Total ← Total - 4
D ENDWHILE
Line C — Total goes 0, -4, -8... and never reaches 10, so the condition Total < 10 is always true and the loop never ends. It should be Total ← Total + 4.
Question 3
A Num ← 0
B WHILE Num < 20 DO
C Num ← Num * 2
D ENDWHILE
E OUTPUT Num
Line A — If Num starts at 0, then 0 * 2 is always 0. Num never reaches 20, so the loop never ends. It should start with Num ← 1.
Question 4
A Tries ← 1
B REPEAT
C Tries ← Tries + 1
D INPUT Guess
E UNTIL Guess = 7
Line A — The counter must start at 0. REPEAT runs the body once before testing, so Tries becomes 1 after the first pass. Starting at 1 would count one attempt too many.
Question 5
A INPUT Mark
B WHILE Mark < 0 AND Mark > 100 DO
C OUTPUT "Try again"
D INPUT Mark
E ENDWHILE
Line B — A mark cannot be below 0 and above 100 at the same time, so the condition is always false. The WHILE loop never runs, even for invalid marks. It should use OR.

Student with a class code? Join your class →