← IGCSE Computer Science Paper 2

Filling a 2D array with nested FOR loops

A FOR loop for the columns placed inside a FOR loop for the rows. The inner loop runs fully for each row, so every element of a 2D array is visited and given a value.

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

What it does: Nested loops finish inside-out: NEXT Column ends the inner loop and then NEXT Row ends the outer loop. A 2D array needs two indexes, the row and the column. Using both loop variables reaches a different element each time. The Column loop must go up to the upper bound, 3. Stopping at 2 would leave the last column unset in every row.

Common mistakes: A FOR loop sets its counter with the assignment arrow ←, not =. The line should be FOR Row ← 1 TO 3. A 2D array element needs both indexes. It should be Grid[Row, Column] ← ' ' so the inner loop sets each column in the row.

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 structure visits every element of a 3 x 3 2D array?
A single FOR Row ← 1 TO 3 loopA FOR Column loop inside a FOR Row loop
Matches
FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Row NEXT Column
↓
The NEXT statements are in the wrong order. The inner loop (Column) must be closed first, then the outer loop (Row).
Applying
Meanings
Term
nested FOR loop
Meaning
A FOR loop for the columns placed inside a FOR loop for the rows. The inner loop runs fully for each row, so every element of a 2D array is visited and given a value.
Practice
FOR Row ← 1 TO 3
Testing
Gaps
Column ← 1 TO 3
Check Answer
Spots
1FOR Row = 1 TO 3
2 FOR Column ← 1 TO 3

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

The 'Octopus' badge — Multi-Tasker, 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 structure visits every element of a 3 x 3 2D array?
A. A FOR Column loop inside a FOR Row loop ✓
B. A single FOR Row ← 1 TO 3 loop
One loop only moves through the rows. You need a second loop inside it for the columns so every row and column pair is reached.
2. Which shows the NEXT statements in the correct order for nested loops?
A. NEXT Column, then NEXT Row ✓
B. NEXT Row, then NEXT Column
The inner loop is closed first, then the outer loop. The loops must not cross over each other.
3. FOR Row ← 1 TO 3 contains FOR Column ← 1 TO 3. How many times does the inner loop body run in total?
A. 9 times (3 for each row) ✓
B. 3 times
The inner loop runs fully for each pass of the outer loop, so 3 x 3 = 9 times. That matches the 9 elements.
4. Which line, inside the inner loop, sets the current element of Board to a space?
A. Board[Row] ← ' '
B. Board[Row, Column] ← ' ' ✓
A 2D array needs two indexes, the row and the column. Using both loop variables reaches a different element each time.
5. Row is 2. What does the inner loop do?
A. Column becomes 2, then Row moves on to 3
B. Column runs 1 to 3 completely, then Row becomes 3 ✓
The inner loop finishes all its repeats for each value of Row before the outer loop moves to the next row.

Match the Pairs

FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 → Outer loop steps through the rows; the inner loop steps through the columns for each row
Board[Row, Column] ← ' ' → Sets the current element, chosen by the row and column loop variables, to a space
The inner loop must be closed first, so its NEXT comes before the NEXT of the outer loop → NEXT Column NEXT Row
A complete nested loop that sets every element of a 3 by 3 array to a space → FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Column NEXT Row
FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Row NEXT Column → The NEXT statements are in the wrong order. The inner loop (Column) must be closed first, then the outer loop (Row).
FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Row] ← ' ' NEXT Column NEXT Row → The Row variable is used for both indexes. The second index should be Column, so only the diagonal elements are set.

Key Term

nested FOR loop
A FOR loop for the columns placed inside a FOR loop for the rows. The inner loop runs fully for each row, so every element of a 2D array is visited and given a value.

Fill in the Blanks

1. FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' '
Hint: Type the keyword that starts the inner count-controlled loop over the columns.
2. FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Column
Hint: Type the loop variable that this inner NEXT statement closes.
3. FOR Row ← 1 TO 3 FOR Column ← 1 TO 3 Board[Row, Column] ← ' '
Hint: Type the upper bound of the row loop so the board has as many rows as it has columns.
4. FOR Column ← 1 TO 3 Board[Row, Column] ← ' ' NEXT Column
Hint: Type the assignment operator that stores a value in the element (type it as <-).
5. Board[Row, Column] ← ' ' NEXT Column NEXT Row
Hint: Type the keyword that ends the outer row loop, after the inner loop has been closed.

Spot the Error

Question 1
A FOR Row = 1 TO 3
B FOR Column ← 1 TO 3
C Board[Row, Column] ← ' '
D NEXT Column
E NEXT Row
Line A — A FOR loop sets its counter with the assignment arrow ←, not =. The line should be FOR Row ← 1 TO 3.
Question 2
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 3
C Grid[Row] ← ' '
D NEXT Column
E NEXT Row
Line C — A 2D array element needs both indexes. It should be Grid[Row, Column] ← ' ' so the inner loop sets each column in the row.
Question 3
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 4
C Board[Row, Column] ← ' '
D NEXT Column
E NEXT Row
Line B — The board only has 3 columns, so the inner loop must stop at 3. Going to 4 causes an index out of range error. It should be FOR Column ← 1 TO 3.
Question 4
A FOR Row ← 1 TO 3
B FOR Column ← 1 TO 3
C Table[Row, Column] ← ' '
D NEXT Row
E NEXT Row
Line D — The inner loop must be closed first, so line 4 should be NEXT Column. NEXT Row comes last, after the inner loop has finished.
Question 5
A FOR Row ← 1 UNTIL 3
B FOR Column ← 1 TO 3
C Map[Row, Column] ← ' '
D NEXT Column
E NEXT Row
Line A — A count-controlled loop uses the keyword TO, not UNTIL. It should be FOR Row ← 1 TO 3.

Student with a class code? Join your class →