← IGCSE Computer Science Paper 2

Tracing a loop in a trace table

Work through the loop body once per pass, recording changed values in a new row each time. Test the condition before the body for WHILE and after it for REPEAT. Check the final row.

Understanding Tracing a loop in a trace table in IGCSE Computer Science Paper 2

What it does: This FOR loop gives the counter values 1, 2, 3 and 4, so the trace table has four rows for the counter. A WHILE loop tests first. If the condition is false at the start, the body never runs. A FOR loop includes both the start and the end value. Counter takes 2, 3, 4 and 5, so there are 4 passes. Record the changed values in a new row each time through the loop body. Skipping passes loses the steps and the marks. Total goes 1, 3, 6 on the three passes. The final row shows N = 3 and Total = 6, so always check the final row.

Common mistakes: Count starts at 4 and the end value is 1, so with the default step of +1 the body never runs and the trace table has no loop rows. It should be FOR Count ← 1 TO 4, which gives Count values 1, 2, 3, 4. A WHILE loop header ends with DO, not THEN (THEN belongs to IF). The condition is tested before each pass of the body, so the line should be WHILE Num < 3 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 216–231) and word search (Modules 188–231).

Take this worksheet further — free with Cloodit

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

Learning
Duo
You are tracing a WHILE loop. When do you test the condition?
After each pass of the bodyBefore each pass of the body
Matches
Trace of REPEAT ... UNTIL Count = 3: test Count = 3 before running the body the first time
↓
A REPEAT loop tests its condition after the body, not before. The body must run once first.
Applying
Meanings
Term
Tracing a loop
Meaning
Work through the loop body once per pass, recording changed values in a new row each time. Test the condition before the body for WHILE and after it for REPEAT. Check the final row.
Practice
Total ← 0
Testing
Gaps
FOR Count ← 1 4
Check Answer
Spots
1Total ← 0
2FOR Count ← 4 TO 1

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

The 'Walrus' badge — Method 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. You are tracing a WHILE loop. When do you test the condition?
A. Before each pass of the body ✓
B. After each pass of the body
A WHILE loop tests first. If the condition is false at the start, the body never runs.
2. Count is 10 before this loop: REPEAT Count ← Count + 1 UNTIL Count > 5. How many times does the body run?
A. Zero times
B. Once ✓
A REPEAT loop runs the body first and tests the condition afterwards. Count becomes 11, so the UNTIL condition is true and the loop stops after one pass.
3. Count is 7 before this loop: WHILE Count < 5 DO. How many times does the body run?
A. Zero times ✓
B. Once
WHILE tests before the body. 7 < 5 is false, so the body is skipped and the trace table has no loop rows.
4. For FOR Counter ← 2 TO 5, which values does Counter take in the trace table?
A. 2, 3, 4
B. 2, 3, 4, 5 ✓
A FOR loop includes both the start and the end value. Counter takes 2, 3, 4 and 5, so there are 4 passes.
5. Which is the correct way to fill in a trace table for a loop?
A. Add a new row on each pass showing the changed values ✓
B. Write one row at the end showing only the final values
Record the changed values in a new row each time through the loop body. Skipping passes loses the steps and the marks.

Match the Pairs

WHILE Count < 3 DO → The condition is tested before the body, so the loop may run zero times. Check it before every pass.
UNTIL Total >= 10 → The condition is tested after the body, so the loop always runs at least once. Stop when it becomes true.
A FOR loop trace has one row per pass, with the counter taking each value from start to end → FOR Count ← 1 TO 4
Record a changed value in a new row each pass, such as a running total going up → Total ← Total + Count
Trace of REPEAT ... UNTIL Count = 3: test Count = 3 before running the body the first time → A REPEAT loop tests its condition after the body, not before. The body must run once first.
Trace of FOR Count ← 1 TO 3: stop the table after the row where Count = 2 → The final row was not checked. The loop runs for Count = 3 too, so the last row is missing.

Key Term

Tracing a loop
Work through the loop body once per pass, recording changed values in a new row each time. Test the condition before the body for WHILE and after it for REPEAT. Check the final row.

Fill in the Blanks

1. FOR Count ← 1 TO 4 OUTPUT Count NEXT Count
Hint: Type the keyword that sets the last counter value, so you know which row of the trace table is final.
2. WHILE Num < 3 DO Num ← Num + 1 ENDWHILE
Hint: Type the keyword for the loop that tests its condition before the body runs, even for the first pass.
3. REPEAT Num ← Num + 2 UNTIL Num >= 7
Hint: Type the keyword that tests the condition after the body, so the body always runs at least once.
4. FOR Count ← 1 TO 3 Total ← Total + Count NEXT Count
Hint: Type the variable that is updated on every pass and gets a new value recorded in each row of the trace table.
5. WHILE Num < 10 DO Num ← Num * 2 ENDWHILE
Hint: Type the arithmetic operator that makes Num double each pass, so the final row shows the value that stops the loop.

Spot the Error

Question 1
A Total ← 0
B FOR Count ← 4 TO 1
C Total ← Total + Count
D NEXT Count
E OUTPUT Total
Line B — Count starts at 4 and the end value is 1, so with the default step of +1 the body never runs and the trace table has no loop rows. It should be FOR Count ← 1 TO 4, which gives Count values 1, 2, 3, 4.
Question 2
A WHILE Num < 3 THEN
B Num ← Num + 1
C OUTPUT Num
D ENDWHILE
Line A — A WHILE loop header ends with DO, not THEN (THEN belongs to IF). The condition is tested before each pass of the body, so the line should be WHILE Num < 3 DO.
Question 3
A Count ← 0
B REPEAT
C Count ← Count + 0
D UNTIL Count = 3
Line C — Adding 0 means Count never changes, so every row of the trace table repeats 0 and the UNTIL test is never true. The loop never ends. It should be Count ← Count + 1.
Question 4
A Sum ← 0
B FOR Item ← 1 TO 3
C Sum ← Sum + Item
D NEXT Sum
E OUTPUT Sum
Line D — NEXT must name the loop counter, which is Item. Sum is the running total, not the counter. It should be NEXT Item.
Question 5
A Count ← 3
B WHILE Count < 0 DO
C Count ← Count - 1
D ENDWHILE
Line B — A WHILE loop tests its condition before the body. Count starts at 3, so 3 < 0 is false and the body never runs, leaving no loop rows in the trace table. Since the body counts down, the condition should be Count > 0.

Student with a class code? Join your class →