← IGCSE Computer Science Paper 2

Using an expression as an array index

An index can be an expression like n+1. The expression is worked out first, then that element is used. It must stay within the array's bounds. Example: n=3 gives Array[4].

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

What it does: The index n + 1 on the left is worked out first, so the value from position n is stored in the next element. The upper bound is 10, so the check n + 1 <= 10 makes sure the calculated index is valid before the element is used. n-1 is worked out as 5 - 1 = 4, so it gives the element before element 5. 1 - 1 = 0, but the lowest valid index is 1, so the index is out of range. If n is 10, then n+1 is 11, which is out of range. Checking n < 10 means n+1 is at most 10.

Common mistakes: To copy a value to the NEXT element the index must be n+1. n-1 is the previous element (2), so the value goes to the wrong place and Names[4] is not set. It should be Names[n+1] ← Names[n]. The previous element is found with n-1. n+1 gives the next element (6), not the previous one. It should be previous ← Score[n-1].

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
n ← 3. Which element does Scores[n+1] refer to?
Scores[3]Scores[4]
Matches
StudentNames[n + 1] ← StudentNames[n] when n = 10 and the array is ARRAY[1:10]
↓
n + 1 works out to 11, which is above the upper bound of 10, so the index is out of range
Applying
Meanings
Term
Array[n+1]
Meaning
An index can be an expression like n+1. The expression is worked out first, then that element is used. It must stay within the array's bounds. Example: n=3 gives Array[4].
Practice
StudentNames[n+1] ← StudentNames[n]
Testing
Gaps
StudentNames[] ← StudentNames[n]
Check Answer
Spots
1n ← 3
2Names[n-1] ← Names[n]

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

The 'Seahorse' badge — Range Rider, 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. n ← 3. Which element does Scores[n+1] refer to?
A. Scores[4] ✓
B. Scores[3]
The expression n+1 is worked out first (3 + 1 = 4), and then element 4 is used.
2. When is the expression n+1 in StudentNames[n+1] worked out?
A. Before the element is chosen ✓
B. After the element has been read
The index expression is always evaluated first. The result tells the computer which element to use.
3. Which line copies the value in element n into the next element?
A. StudentNames[n+1] ← StudentNames[n] ✓
B. StudentNames[n] ← StudentNames[n+1]
The arrow points to the destination. The first line writes element n into element n+1. The second does the opposite and copies from the next element back into n.
4. n ← 5. Which element does StudentNames[n-1] refer to?
A. Element 6, the next one
B. Element 4, the previous one ✓
n-1 is worked out as 5 - 1 = 4, so it gives the element before element 5.
5. An array is declared as ARRAY[1:10] OF INTEGER and n ← 10. What happens with Marks[n+1]?
A. It is fine and the array grows to hold element 11
B. The index is 11, which is outside the bounds, so it is an error ✓
An array has a fixed length. The upper bound is 10, so index 11 is out of range.

Match the Pairs

Scores[n + 1] → Works out n + 1 first, then uses the element at that index, the one after position n
Scores[n - 1] → Works out n - 1 first, then uses the element at that index, the one before position n
Copy the value in element n into the next element along → StudentNames[n + 1] ← StudentNames[n]
Check that n + 1 stays within the bounds of an array declared ARRAY[1:10] before using it → IF n + 1 <= 10 THEN StudentNames[n + 1] ← StudentNames[n] ENDIF
StudentNames[n + 1] ← StudentNames[n] when n = 10 and the array is ARRAY[1:10] → n + 1 works out to 11, which is above the upper bound of 10, so the index is out of range
StudentNames[n - 1] ← StudentNames[n] when n = 1 and the array is ARRAY[1:10] → n - 1 works out to 0, which is below the lower bound of 1, so the index is out of range

Key Term

Array[n+1]
An index can be an expression like n+1. The expression is worked out first, then that element is used. It must stay within the array's bounds. Example: n=3 gives Array[4].

Fill in the Blanks

1. StudentNames[n+1] ← StudentNames[n]
Hint: Type the expression that picks the element after the one at index n.
2. Diff ← Marks[n] - Marks[n-1]
Hint: Type the expression that picks the element just before index n.
3. n ← 3 OUTPUT Scores[n+1] // Shows element Scores[4]
Hint: Work out the expression first. Type the index number it gives when n is 3.
4. IF n + 1 <= 10 THEN Names[n + 1] ← Names[n] ENDIF
Hint: Type the comparison operator that keeps the index within the upper bound of ARRAY[1:10].
5. // Names is ARRAY[1:10] FOR n ← 1 TO 9 Names[n + 1] ← Names[n]
Hint: Type the last value of n that keeps n+1 inside the upper bound of the array.

Spot the Error

Question 1
A n ← 3
B Names[n-1] ← Names[n]
C OUTPUT Names[4]
Line B — To copy a value to the NEXT element the index must be n+1. n-1 is the previous element (2), so the value goes to the wrong place and Names[4] is not set. It should be Names[n+1] ← Names[n].
Question 2
A n ← 5
B previous ← Score[n+1]
C OUTPUT previous
Line B — The previous element is found with n-1. n+1 gives the next element (6), not the previous one. It should be previous ← Score[n-1].
Question 3
A n ← 4
B IF n+1 <= 5 THEN
C Names[n]+1 ← "Zoe"
D ENDIF
Line C — The +1 must be inside the square brackets so that the index expression n+1 is worked out before the element is chosen. It should be Names[n+1] ← "Zoe".
Question 4
A n ← 3
B Names(n+1) ← Names[n]
C OUTPUT Names[n+1]
Line B — Array indexes are written in square brackets, not round brackets. It should be Names[n+1] ← Names[n].
Question 5
A n ← 2.5
B Total ← Marks[n] + Marks[n+1]
C OUTPUT Total
Line A — An array index must be a whole number. With n as 2.5, n+1 is 3.5, which is not a valid element. It should be n ← 2.

Student with a class code? Join your class →