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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
trace tableFill in the Blanks
Spot the Error
Student with a class code? Join your class →