← IGCSE Computer Science Paper 2

Tracing a FOR loop

To trace a FOR loop, give the loop variable each value from start to end in turn, run the loop body once for each value, and record every pass in its own row of the trace table.

Understanding Tracing a FOR loop in IGCSE Computer Science Paper 2

What it does: This loop has 4 passes, so the trace table has 4 rows, with Count as 1, 2, 3 and 4 and the OUTPUT column showing 2, 4, 6 and 8. STEP -1 subtracts 1 each pass, so the loop variable column goes 5, 4, 3, 2, 1 and the loop body runs 5 times. The loop variable takes every value from the start to the end, and the end value is included. Count goes down by 4 each time: 10, 6, 2. The value 2 equals the end value, so it is still included. After pass 1, Total = 0 + 1 = 1. After pass 2, Total = 1 + 2 = 3.

Common mistakes: Total must add each loop value, not copy it. Total ← Count overwrites Total every pass, so the trace ends with 4 instead of 10. It should be Total ← Total + Count. The loop variable must run to the end value 5, giving five passes. TO 4 stops one pass early, so the trace table would have only 4 rows. It should be FOR Count ← 1 TO 5.

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 table is being filled in for FOR Count ← 1 TO 5. Which values does Count take, in order?
1, 2, 3, 41, 2, 3, 4, 5
Matches
FOR Count ← 1 TO 3 gives rows with Count = 1, 2, 3, 4
↓
The trace goes one value too far. The loop variable stops at the end value 3, so there are only 3 passes and no row for 4.
Applying
Meanings
Term
FOR Count ← 1 TO 5
Meaning
To trace a FOR loop, give the loop variable each value from start to end in turn, run the loop body once for each value, and record every pass in its own row of the trace table.
Practice
Total ← 0
Testing
Gaps
FOR Count ← 1 5
Check Answer
Spots
2FOR Count ← 1 TO 4
3 Total ← Count

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

The 'Starfish' badge — Five-Point Star, 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 table is being filled in for FOR Count ← 1 TO 5. Which values does Count take, in order?
A. 1, 2, 3, 4
B. 1, 2, 3, 4, 5 ✓
The loop variable takes every value from the start to the end, and the end value is included.
2. How many times does the loop body run for FOR Num ← 3 TO 7?
A. 5 times (Num = 3, 4, 5, 6, 7) ✓
B. 4 times (7 - 3)
Count the values, not the difference. 3, 4, 5, 6 and 7 gives five passes, so five rows.
3. When tracing a FOR loop, how are the passes recorded in the trace table?
A. Each pass gets its own row ✓
B. The whole loop is written in one row
Every pass through the loop body is recorded in its own row so you can see how the values change.
4. Should the loop variable have its own column in a trace table?
A. No, the FOR loop controls it so it is not recorded
B. Yes, the loop variable is a variable and gets a column ✓
The loop variable changes on every pass, so it needs a column. This shows which pass you are on.
5. Which values does X take in FOR X ← 0 TO 10 STEP 5?
A. 0, 5, 10 ✓
B. 0, 5, 10, 15
X starts at 0 and goes up by 5 each time. The next value, 15, would pass the end value of 10, so the loop stops.

Match the Pairs

FOR Count ← 1 TO 3 → Trace this loop with Count = 1, then 2, then 3, running the loop body once for each value and using one row per pass.
FOR Num ← 2 TO 10 STEP 4 → Trace this loop with Num = 2, then 6, then 10, because STEP 4 adds 4 each time, giving 3 passes.
Every pass of the loop body gets its own row, with the loop variable column showing the current value for that pass. → FOR Count ← 1 TO 4 OUTPUT Count * 2 NEXT Count
A FOR loop that counts down uses a negative STEP, so the loop variable is traced with 5, 4, 3, 2, 1. → FOR Num ← 5 TO 1 STEP -1 OUTPUT Num NEXT Num
FOR Count ← 1 TO 3 gives rows with Count = 1, 2, 3, 4 → The trace goes one value too far. The loop variable stops at the end value 3, so there are only 3 passes and no row for 4.
FOR Num ← 1 TO 9 STEP 2 gives rows with Num = 1, 2, 3, 4, 5 → STEP 2 was ignored. The loop variable should go up by 2 each pass, giving 1, 3, 5, 7, 9 and not every number.

Key Term

FOR Count ← 1 TO 5
To trace a FOR loop, give the loop variable each value from start to end in turn, run the loop body once for each value, and record every pass in its own row of the trace table.

Fill in the Blanks

1. FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Hint: Type the keyword that sits between the start value and the end value of the loop variable.
2. FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Hint: Type the keyword that marks the end of the loop body and moves on to the next value.
3. FOR Count ← 1 TO 10 STEP 3 OUTPUT Count NEXT Count
Hint: Type the STEP size so the trace table rows show the loop variable going 1, 4, 7, 10.
4. FOR Num ← 1 TO 3 Total ← Total + Num NEXT Num
Hint: Type the loop variable, whose current value is added to the total on each pass.
5. FOR Count ← 5 TO 1 STEP -1 OUTPUT Count NEXT Count
Hint: Type the STEP value that makes the loop variable go down by one on each pass.

Spot the Error

Question 1
A Total ← 0
B FOR Count ← 1 TO 4
C Total ← Count
D NEXT Count
E OUTPUT Total
Line C — Total must add each loop value, not copy it. Total ← Count overwrites Total every pass, so the trace ends with 4 instead of 10. It should be Total ← Total + Count.
Question 2
A // Count takes 1, 2, 3, 4, 5
B FOR Count ← 1 TO 4
C OUTPUT Count
D NEXT Count
Line B — The loop variable must run to the end value 5, giving five passes. TO 4 stops one pass early, so the trace table would have only 4 rows. It should be FOR Count ← 1 TO 5.
Question 3
A FOR Num ← 1 TO 3
B OUTPUT Num * 2
C NEXT Count
D OUTPUT "Finished"
Line C — NEXT must name the same loop variable as the FOR line. The loop variable is Num, so the line should be NEXT Num.
Question 4
A FOR Count = 1 TO 3
B OUTPUT Count
C NEXT Count
Line A — A FOR loop gives its starting value to the loop variable with the assignment arrow ←, not =. The line should be FOR Count ← 1 TO 3.
Question 5
A Product ← 1
B FOR Num ← 1 TO 3
C Product ← Product * Num
D ENDFOR
E OUTPUT Product
Line D — Cambridge pseudocode ends a FOR loop with NEXT followed by the loop variable, not ENDFOR. The line should be NEXT Num.

Student with a class code? Join your class →