← IGCSE Computer Science Paper 2

Displaying a 2D array with nested loops

Placed inside nested FOR loops (outer loop for rows, inner loop for columns), this line displays each element of a 2D array once, in row order.

Understanding Displaying a 2D array with nested loops in IGCSE Computer Science Paper 2

What it does: The inner FOR loop steps through the columns, and the OUTPUT line shows one element per pass, giving one output per element. Row changes on each pass while the column stays at 3, so only the elements in column 3 are output. Cambridge pseudocode uses one pair of square brackets with the indexes separated by a comma: Grid[Row, Column]. In nested loops that display a 2D array, the outer loop counts the rows, so it runs once per row.

Common mistakes: The outer loop for the rows is missing the assignment arrow. It should be FOR Row ← 1 TO 3. The inner loop is FOR Col, so it must end with NEXT Col. NEXT Row belongs on the last line, closing the outer loop.

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 469–478) and word search (Modules 459–478).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which nested loop layout displays a 2D array in row order?
Outer loop for columns, inner loop for rowsOuter loop for rows, inner loop for columns
Matches
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row] NEXT Column NEXT Row
↓
The OUTPUT line gives only one index. A 2D array needs both the row and the column, so it should be Grid[Row, Column].
Applying
Meanings
Term
OUTPUT Name[Row, Column]
Meaning
Placed inside nested FOR loops (outer loop for rows, inner loop for columns), this line displays each element of a 2D array once, in row order.
Practice
FOR Row ← 1 TO 3
Testing
Gaps
Grid[Row, Column]
Check Answer
Spots
1FOR Row 1 TO 3
2 FOR Col ← 1 TO 4

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

The 'Jellyfish' badge — Flow Controller, 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. Which nested loop layout displays a 2D array in row order?
A. Outer loop for columns, inner loop for rows
B. Outer loop for rows, inner loop for columns ✓
The inner loop changes fastest. With columns inside, every element of row 1 is shown, then row 2, and so on, which is row order.
2. Where should OUTPUT Grid[Row, Column] go to display every element once?
A. Inside the inner loop ✓
B. After the outer loop has finished
The OUTPUT line must run for every row and column pair, so it goes inside the inner loop. After the loops, Row and Column would only hold their last values.
3. A 2D array Grid[1:3, 1:4] is displayed with nested loops. How many times does OUTPUT Grid[Row, Column] run?
A. 12 times ✓
B. 7 times
3 rows x 4 columns = 12 elements and each one is output once. Adding the sizes (3 + 4 = 7) is a common mistake.
4. Which FOR line is the inner loop for Seats[1:5, 1:8] when the outer loop uses Row?
A. FOR Column ← 1 TO 8 ✓
B. FOR Column ← 1 TO 5
The inner loop covers the columns, so it must run to the upper bound of the second index, 8. The 5 is the number of rows.
5. Which code outputs ONLY row 2 of Grid[1:3, 1:4]?
A. FOR Row ← 1 TO 3 OUTPUT Grid[Row, 2] NEXT Row
B. FOR Column ← 1 TO 4 OUTPUT Grid[2, Column] NEXT Column ✓
To output one row, keep the row index fixed at 2 and loop through the columns. The wrong answer fixes the column, so it outputs column 2 instead.

Match the Pairs

FOR Row ← 1 TO 3 → The outer loop: it moves through each row of the 2D array, one row at a time.
OUTPUT Grid[2, Column] → Inside a loop over Column, this displays every element of row 2 only.
The inner loop moves across the columns, and OUTPUT inside it displays one element each time round. → FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column] NEXT Column
To display only column 3 of a 2D array, loop through the rows and keep the column index fixed. → FOR Row ← 1 TO 3 OUTPUT Grid[Row, 3] NEXT Row
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row] NEXT Column NEXT Row → The OUTPUT line gives only one index. A 2D array needs both the row and the column, so it should be Grid[Row, Column].
FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column] NEXT Column → The outer loop has no NEXT Row, so it is never closed. NEXT Row must be added after NEXT Column.

Key Term

OUTPUT Name[Row, Column]
Placed inside nested FOR loops (outer loop for rows, inner loop for columns), this line displays each element of a 2D array once, in row order.

Fill in the Blanks

1. FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column]
Hint: Type the keyword that displays each element on the screen.
2. FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Grid[Row, Column]
Hint: Type the inner loop variable that moves across the columns.
3. FOR Row ← 1 TO 3 OUTPUT Grid[Row, 2] NEXT Row
Hint: Type the loop variable that steps down the rows to output a single column.
4. OUTPUT Grid[Row, Column] NEXT Column NEXT Row
Hint: Type the variable of the inner loop, which must be closed first.
5. FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT Seats[Row, Column]
Hint: Type the keyword that separates the start and end values of the inner loop.

Spot the Error

Question 1
A FOR Row 1 TO 3
B FOR Col ← 1 TO 4
C OUTPUT Grid[Row, Col]
D NEXT Col
E NEXT Row
Line A — The outer loop for the rows is missing the assignment arrow. It should be FOR Row ← 1 TO 3.
Question 2
A FOR Row ← 1 TO 3
B FOR Col ← 1 TO 4
C OUTPUT Grid[Row, Col]
D NEXT Row
E NEXT Row
Line D — The inner loop is FOR Col, so it must end with NEXT Col. NEXT Row belongs on the last line, closing the outer loop.
Question 3
A FOR Student ← 1 TO 4
B FOR Test ← 1 TO 3
C OUTPUT Scores[Test, Student]
D NEXT Test
E NEXT Student
Line C — The row index must come first and the column index second. Scores[Test, Student] swaps them, so the elements are not shown in row order. It should be Scores[Student, Test].
Question 4
A FOR Row ← 1 TO 5
B FOR Row ← 1 TO 8
C OUTPUT Seats[Row, Seat]
D NEXT Seat
E NEXT Row
Line B — The inner loop reuses the variable Row, which clashes with the outer loop. The inner loop for the columns needs its own variable: FOR Seat ← 1 TO 8.
Question 5
A FOR Day ← 1 TO 7
B FOR Hour ← 1 TO 3
C OUTPUT Temp[Day]
D NEXT Hour
E NEXT Day
Line C — A 2D array needs two indexes, a row and a column. Temp[Day] gives only one. It should be OUTPUT Temp[Day, Hour].

Student with a class code? Join your class →