← IGCSE Computer Science Paper 2

Reading values into a 2D array with INPUT in nested loops

Inside nested FOR loops, this reads a typed value into one cell of a 2D array. The inner loop fills a whole row before the outer loop moves to the next row.

Understanding Reading values into a 2D array with INPUT in nested loops in IGCSE Computer Science Paper 2

What it does: The prompt uses the loop variables Student and Test, so the message changes each time to match the cell about to be filled by INPUT. The prompt must appear before the INPUT so the user knows what to type. If INPUT comes first, the program waits for a value with no prompt shown. The first three values fill Marks[1,1], Marks[1,2], Marks[1,3]. The 4th starts row 2, so it goes in Marks[2,1]. There is no 4th test in row 1. The inner loop controls the column. It finishes all of its columns for the current row before the outer loop changes the row.

Common mistakes: A FOR loop sets its counter with the assignment arrow, not =. The line should be FOR Student ← 1 TO 3. Marks is a 2D array, so each cell needs a row index and a column index. It should be INPUT Marks[Student, Test].

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 statement reads a typed value into one cell of a 2D array called Marks?
INPUT Marks[Student, Test]INPUT Marks
Matches
FOR Student ← 1 TO 30\n INPUT Marks[Student, Test]\nNEXT Student
↓
There is no inner loop for Test, so Test never changes. Only one column is filled for each student, and the other columns stay empty.
Applying
Meanings
Term
INPUT Marks[Student, Test]
Meaning
Inside nested FOR loops, this reads a typed value into one cell of a 2D array. The inner loop fills a whole row before the outer loop moves to the next row.
Practice
FOR Student ← 1 TO 3
Testing
Gaps
Marks[Student, Test]
Check Answer
Spots
1FOR Student = 1 TO 3
2 FOR Test ← 1 TO 2

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

The 'Warthog' badge — Definition Digger, 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 statement reads a typed value into one cell of a 2D array called Marks?
A. INPUT Marks[Student, Test] ✓
B. INPUT Marks
A 2D array cell needs both indexes, row then column, so Marks[Student, Test] picks out one cell. INPUT Marks alone does not say which cell to fill.
2. Marks[Student, Test] is read inside nested loops with Student as the OUTER loop and Test as the INNER loop. Which order are the cells filled in?
A. One whole column at a time: Test 1 for every student, then Test 2 for every student
B. One whole row at a time: Student 1 tests 1 to 3, then Student 2 tests 1 to 3 ✓
The inner loop runs completely for each value of the outer loop. With Student outside, a whole row is filled before moving to the next student.
3. Which loop structure correctly reads every cell of Marks[1:4, 1:3] (4 students, 3 tests)?
A. FOR Student ← 1 TO 4 FOR Test ← 1 TO 3 INPUT Marks[Student, Test] NEXT Test NEXT Student ✓
B. FOR Student ← 1 TO 4 INPUT Marks[Student, Test] NEXT Student
You need a nested loop. A single loop only changes Student, and Test has no value, so most cells would never be filled.
4. Which OUTPUT prompt, placed before INPUT in the inner loop, tells the user which cell is being entered?
A. OUTPUT "Enter a mark"
B. OUTPUT "Enter mark for student ", Student, " test ", Test ✓
Showing Student and Test in the prompt tells the user the row and column of the mark they are typing. A fixed message gives no clue which cell is being filled.
5. In which order should these lines be placed inside the inner loop?
A. OUTPUT the prompt first, then INPUT Marks[Student, Test] ✓
B. INPUT Marks[Student, Test] first, then OUTPUT the prompt
The prompt must appear before the INPUT so the user knows what to type. If INPUT comes first, the program waits for a value with no prompt shown.

Match the Pairs

INPUT Marks[Student, Test] → Reads a typed value into one cell of the 2D array, chosen by the current row (Student) and column (Test).
FOR Test ← 1 TO 3\n INPUT Marks[Student, Test]\nNEXT Test → The inner loop reads three values into one row, one for each test, before the outer loop moves to the next student.
A prompt can show which row and column is being entered, so the user knows which mark to type. → OUTPUT "Enter mark for student ", Student, " test ", Test
To enter values column by column instead of row by row, swap the loops so the outer loop controls the test (column). → FOR Test ← 1 TO 3\n FOR Student ← 1 TO 30\n INPUT Marks[Student, Test]
FOR Student ← 1 TO 30\n INPUT Marks[Student, Test]\nNEXT Student → There is no inner loop for Test, so Test never changes. Only one column is filled for each student, and the other columns stay empty.
FOR Student ← 1 TO 30\n FOR Test ← 1 TO 3\n INPUT Marks[Test, Student] → The indexes are in the wrong order. Marks[Test, Student] treats Test as the row and Student as the column, which does not match the Marks[Student, Test] layout and can go out of range.

Key Term

INPUT Marks[Student, Test]
Inside nested FOR loops, this reads a typed value into one cell of a 2D array. The inner loop fills a whole row before the outer loop moves to the next row.

Fill in the Blanks

1. FOR Student ← 1 TO 3 FOR Test ← 1 TO 4 INPUT Marks[Student, Test]
Hint: Type the keyword that reads a typed value into the current cell.
2. FOR Student ← 1 TO 3 FOR Test ← 1 TO 4 INPUT Marks[Student, Test]
Hint: Type the variable that gives the column index, which is the inner loop counter.
3. FOR Test ← 1 TO 4 OUTPUT "Row ", Student, " column ", Test INPUT Marks[Student, Test]
Hint: Type the variable that tells the user which column is being entered.
4. FOR Test ← 1 TO 4 FOR Student ← 1 TO 3 INPUT Marks[Student, Test]
Hint: The loops are swapped to enter column by column. Type the row variable that now runs in the inner loop.
5. FOR Student ← 1 TO 3 FOR Test ← 1 TO 4 INPUT Marks[Student, Test]
Hint: Type the keyword that separates the start and end values in a FOR loop.

Spot the Error

Question 1
A FOR Student = 1 TO 3
B FOR Test ← 1 TO 2
C INPUT Marks[Student, Test]
D NEXT Test
E NEXT Student
Line A — A FOR loop sets its counter with the assignment arrow, not =. The line should be FOR Student ← 1 TO 3.
Question 2
A FOR Student ← 1 TO 3
B FOR Test ← 1 TO 2
C INPUT Marks[Student]
D NEXT Test
E NEXT Student
Line C — Marks is a 2D array, so each cell needs a row index and a column index. It should be INPUT Marks[Student, Test].
Question 3
A FOR Student ← 3 TO 1
B FOR Test ← 1 TO 2
C INPUT Marks[Student, Test]
D NEXT Test
E NEXT Student
Line A — The start value is bigger than the end value, so the loop never runs and no marks are read. It should be FOR Student ← 1 TO 3.
Question 4
A FOR Row ← 1 TO 3
B FOR Row ← 1 TO 4
C INPUT Grid[Row, Col]
D NEXT Col
E NEXT Row
Line B — The inner loop reuses the outer loop's counter Row, which breaks the nesting. The inner loop must use its own counter: FOR Col ← 1 TO 4.
Question 5
A FOR Student ← 1 TO 3
B FOR Test ← 1 TO 2
C INPUT Marks[Student, Test]
D NEXT Student
E NEXT Student
Line D — Nested loops must be closed in reverse order, so the inner loop is closed first. Line 4 should be NEXT Test.

Student with a class code? Join your class →