Traversing a 2D array with nested loops
Reads one element of a 2D array. An outer loop steps through each row and an inner loop steps through each column, so every element in the grid gets visited.
Understanding Traversing a 2D array with nested loops in VCE Software Development U3O1
What it does: The inner loop prints all items in one row on the same line using end=' ', and the bare print() after it starts a new line for the next row. Nested loops visit every element of the grid, and the if statement compares each element grid[r][c] with the target value being searched for. The outer loop picks the row and the inner loop picks each column in that row. Without an inner loop, col is never defined and only one position per row would be read. Resetting total to 0 for each row stops one row's total carrying into the next. If it is set only once at the start, the totals keep adding up across all rows.
Common mistakes: The grid has only 2 rows, so the outer loop must use range(2). range(3) makes r reach 2, and grid[2] causes an IndexError. It should be: for r in range(2):. The row total must start at 0 for every row. Starting at 1 makes every row total one too high. It should be: total = 0.
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 72–96) and word search (Modules 72–113).
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
grid[row][col]Fill in the Blanks
Spot the Error
Student with a class code? Join your class →