← IGCSE Computer Science Paper 2

Using a variable as an array index

A variable inside the brackets picks the element whose index matches its value. Change Index (an INTEGER) and the same statement reaches a different element of Marks.

Understanding Using a variable as an array index in IGCSE Computer Science Paper 2

What it does: The statement OUTPUT Marks[Index] is identical both times, but it shows element 2 first and element 5 second because Index has changed. An index is a whole number position, so the variable placed inside the square brackets is declared as INTEGER. Changing the variable changes which element the same statement reaches. Quotes turn Index into text. A variable must be written without quotes so its value is used.

Common mistakes: A variable used as an array index must be an INTEGER. Index is declared as STRING here. It should be DECLARE Index : INTEGER. Array indexes go inside square brackets, not round brackets. It should be Marks[Index + 1] ← 70.

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 449–458) and word search (Modules 439–458).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Index ← 3 then OUTPUT Marks[Index]. Which element is displayed?
Marks[3], because Index holds 3Marks[Index], because the name Index is used as the position
Matches
DECLARE Index : STRING Index ← 3 OUTPUT Marks[Index]
↓
Index is declared as STRING, but a variable used as an array index must be an INTEGER whole number.
Applying
Meanings
Term
Marks[Index]
Meaning
A variable inside the brackets picks the element whose index matches its value. Change Index (an INTEGER) and the same statement reaches a different element of Marks.
Practice
DECLARE Marks : ARRAY[1:5] OF INTEGER
Testing
Gaps
OUTPUT Marks[]
Check Answer
Spots
1DECLARE Index : STRING
2Index ← 3

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

The 'Abyssal Dragon' badge — Deep Sea Legend, 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. Index ← 3 then OUTPUT Marks[Index]. Which element is displayed?
A. Marks[Index], because the name Index is used as the position
B. Marks[3], because Index holds 3 ✓
The variable is replaced by its value, so Marks[Index] reaches the element at index 3.
2. The same statement OUTPUT Marks[Index] runs twice. Index is 2 the first time and 5 the second time. What happens?
A. It displays Marks[2] and then Marks[5] ✓
B. It displays Marks[2] both times
Changing the variable changes which element the same statement reaches.
3. Which declaration suits a variable used as an array index?
A. DECLARE Index : INTEGER ✓
B. DECLARE Index : STRING
An index is a whole number position, so the variable must be an INTEGER.
4. Which line stores the value 72 in the element at the position held by Index?
A. Index ← Marks[72]
B. Marks[Index] ← 72 ✓
Writing into an element puts the array element on the left of the arrow. The variable inside the brackets picks the element.
5. Index ← 4. Which line copies the element at that position into Score?
A. Score ← Marks[Index] ✓
B. Marks[Index] ← Score
Reading a value puts Marks[Index] on the right of the arrow. The other line would overwrite the element.

Match the Pairs

Index ← 3 OUTPUT Marks[Index] → Displays the value stored in element 3 of Marks, because Index currently holds 3
Marks[Index] ← 50 → Stores 50 in whichever element Index currently points to
Changing the value of the index variable makes the same statement reach a different element → Index ← 2 OUTPUT Marks[Index] Index ← 5 OUTPUT Marks[Index]
The variable used as an index must be an INTEGER → DECLARE Index : INTEGER
DECLARE Index : STRING Index ← 3 OUTPUT Marks[Index] → Index is declared as STRING, but a variable used as an array index must be an INTEGER whole number.
Index ← 4 OUTPUT Marks["Index"] → Index is in quotation marks, so it is treated as text, not as the variable holding 4. It should be Marks[Index].

Key Term

Marks[Index]
A variable inside the brackets picks the element whose index matches its value. Change Index (an INTEGER) and the same statement reaches a different element of Marks.

Fill in the Blanks

1. Index ← 3 OUTPUT Marks[Index]
Hint: Type the INTEGER variable that goes inside the square brackets to choose the element.
2. DECLARE Index : INTEGER Index ← 3 Marks[Index] ← 85
Hint: Type the data type an index variable must have.
3. Index ← 3 Index ← Index + 1 OUTPUT Marks[Index]
Hint: Type the operator that moves the index on to the next element.
4. Index ← 3 Marks[Index] ← 85
Hint: Type the assignment symbol that writes a value into the element.
5. Index ← 5 OUTPUT Marks[Index]
Hint: Type the keyword that displays the value read from the element.

Spot the Error

Question 1
A DECLARE Index : STRING
B Index ← 3
C Marks[Index] ← 75
D OUTPUT Marks[Index]
Line A — A variable used as an array index must be an INTEGER. Index is declared as STRING here. It should be DECLARE Index : INTEGER.
Question 2
A Index ← 2
B Marks[Index] ← 60
C Marks(Index + 1) ← 70
D OUTPUT Marks[Index + 1]
Line C — Array indexes go inside square brackets, not round brackets. It should be Marks[Index + 1] ← 70.
Question 3
A Index ← 4
B Total ← Marks[Idx]
C OUTPUT Total
Line B — The index variable is called Index, but Idx has never been given a value. The brackets must use the same variable name: Total ← Marks[Index].
Question 4
A Index ← 2
B Marks[Index] ← 77
C Index ← 3
D Marks["Index"] ← 64
Line D — Quotes turn Index into a piece of text, not the variable. The variable name must be written without quotes: Marks[Index] ← 64.
Question 5
A DECLARE Pos : INTEGER
B Pos ← 2.5
C Marks[Pos] ← 90
D OUTPUT Marks[Pos]
Line B — Pos is an INTEGER and is used as an index, so it must be a whole number. An index such as 2.5 does not exist. It should be Pos ← 2.

Student with a class code? Join your class →