← IGCSE Computer Science Paper 2

Tracing nested loops over a 2D array

Tracks Row, Column and the element changed. The outer loop variable stays fixed while the inner one runs through all its values. Total passes = rows x columns.

Understanding Tracing nested loops over a 2D array in IGCSE Computer Science Paper 2

What it does: The element changed is Grid[Row, Col]. Each pass changes one cell, and its Row and Col values are recorded in the trace table. With Row as the outer loop, the order is [1,1] [1,2] [1,3] then [2,1] [2,2] [2,3]. The inner variable Col changes fastest. Total passes = rows x columns. The inner loop runs fully (4 times) for each of the 3 rows. Column changes first while Row stays at 1. The pairs go 1,1 then 1,2 then 1,3 and only then Row becomes 2. The elements are visited row by row. The whole of row 1 is done before row 2 starts.

Common mistakes: The first index is the row and the second is the column, so it must be G[Row, Col]. With G[Col, Row], Col reaches 3 and goes past the 2 rows, and the trace table would record the wrong element. The array has only 3 rows, so the outer loop must stop at 3. Going to 4 gives 4 x 4 = 16 passes instead of 3 x 4 = 12, and Row = 4 is out of range.

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 479–488) and word search (Modules 479–499).

Take this worksheet further — free with Cloodit

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

Learning
Duo
In a trace table for nested loops over a 2D array, which variable keeps the same value while the inner loop runs?
The outer loop variable (Row)The inner loop variable (Column)
Matches
Row 1, Col 1 then Row 2, Col 1 then Row 3, Col 1
↓
The trace table changes Row on every pass. The outer variable should stay fixed while Col runs through all its values before Row changes.
Applying
Meanings
Term
trace table
Meaning
Tracks Row, Column and the element changed. The outer loop variable stays fixed while the inner one runs through all its values. Total passes = rows x columns.
Practice
DECLARE Grid : ARRAY[1:2, 1:3] OF INTEGER
Testing
Gaps
FOR ← 1 TO 3
Check Answer
Spots
2 FOR Col ← 1 TO 3
3 G[Col, Row] ← Row * Col

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

The 'Zebra' badge — Pattern Matcher, 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. In a trace table for nested loops over a 2D array, which variable keeps the same value while the inner loop runs?
A. The outer loop variable (Row) ✓
B. The inner loop variable (Column)
The outer variable only changes after the inner loop has finished. The inner variable (Column) changes on every pass.
2. A 2D array has 3 rows and 4 columns. A nested loop visits every element once. How many passes does the inner loop body make in total?
A. 7 (3 + 4)
B. 12 (3 x 4) ✓
Total passes = rows x columns. The inner loop runs fully (4 times) for each of the 3 rows.
3. FOR Row <- 1 TO 2 with FOR Column <- 1 TO 3 inside it. Which Row and Column pair is the SECOND one written in the trace table?
A. Row 2, Column 1
B. Row 1, Column 2 ✓
Column changes first while Row stays at 1. The pairs go 1,1 then 1,2 then 1,3 and only then Row becomes 2.
4. Which shows the correct order of the first three Row, Column pairs for FOR Row <- 1 TO 2 and FOR Column <- 1 TO 3?
A. 1,1 then 1,2 then 1,3 ✓
B. 1,1 then 2,1 then 1,2
The elements are visited row by row. The whole of row 1 is done before row 2 starts.
5. When does the Row value change in the trace table of a nested loop?
A. Only after Column has finished its last value ✓
B. After every single pass of the inner loop
The outer loop moves on only when the inner loop has completed. Then Column restarts from its lower bound.

Match the Pairs

FOR Row ← 1 TO 3 FOR Col ← 1 TO 4 → The loops make 3 x 4 = 12 passes. Row stays fixed while Col runs 1 to 4, so the trace table needs 12 rows.
Row = 2 | Col = 1, 2, 3 → A trace table shows the outer variable fixed at 2 while the inner variable runs through all of its values.
A trace table has a column for the element changed, so the student records which array cell is written on each pass → Grid[Row, Col] ← Row * Col
The order elements are visited: the outer loop moves down the rows, and the inner loop moves across the columns of each row → FOR Row ← 1 TO 2 FOR Col ← 1 TO 3
Row 1, Col 1 then Row 2, Col 1 then Row 3, Col 1 → The trace table changes Row on every pass. The outer variable should stay fixed while Col runs through all its values before Row changes.
2 rows x 3 columns = 5 passes → The total passes were added instead of multiplied. It should be 2 x 3 = 6 passes, so the trace table needs 6 lines.

Key Term

trace table
Tracks Row, Column and the element changed. The outer loop variable stays fixed while the inner one runs through all its values. Total passes = rows x columns.

Fill in the Blanks

1. FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 Grid[Row, Column] ← Row + Column
Hint: Type the name of the inner loop variable, the one that runs through all its values while Row stays fixed.
2. Rows ← 2 Columns ← 3 Passes ← Rows * Columns
Hint: Type the operator that works out total passes from the number of rows and columns.
3. FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 Grid[Row, Column] ← 0
Hint: Type the variable that gives the row index of the element changed. It is the outer loop variable and stays fixed during the inner loop.
4. FOR Row ← 1 TO 2 FOR Column ← 1 TO 3 // Last element visited: Grid[2, 3]
Hint: Type the final value the inner loop variable reaches, so the last element in the visiting order is identified.
5. Grid[Row, Column] ← 0 NEXT Column NEXT Row
Hint: Type the loop variable that finishes first. The inner loop completes all its values before the outer variable moves on.

Spot the Error

Question 1
A FOR Row ← 1 TO 2
B FOR Col ← 1 TO 3
C G[Col, Row] ← Row * Col
D NEXT Col
E NEXT Row
Line C — The first index is the row and the second is the column, so it must be G[Row, Col]. With G[Col, Row], Col reaches 3 and goes past the 2 rows, and the trace table would record the wrong element.
Question 2
A // 3 rows, 4 columns: 12 passes
B FOR Row ← 1 TO 4
C FOR Col ← 1 TO 4
D Grid[Row, Col] ← 0
E NEXT Col
F NEXT Row
Line B — The array has only 3 rows, so the outer loop must stop at 3. Going to 4 gives 4 x 4 = 16 passes instead of 3 x 4 = 12, and Row = 4 is out of range.
Question 3
A FOR Row ← 1 TO 2
B FOR Col ← 1 TO 2
C T[Row, Col] ← Row + Col
D NEXT Row
E NEXT Row
Line D — Loops must be closed in the reverse order they were opened. The inner loop must end with NEXT Col before the outer loop ends with NEXT Row.
Question 4
A FOR Row ← 1 TO 3
B FOR Row ← 1 TO 2
C OUTPUT Row, Col
D NEXT Col
E NEXT Row
Line B — The inner loop reuses the variable Row, so the outer variable would not stay fixed. The inner loop must use its own variable: FOR Col ← 1 TO 2.
Question 5
A S ← 1
B FOR Row ← 1 TO 2
C FOR Col ← 1 TO 3
D S ← S + A[Row, Col]
E NEXT Col
F NEXT Row
Line A — The total must start at 0. Starting at 1 makes every value in the S column of the trace table, and the final total, one too big.

Student with a class code? Join your class →