← IGCSE Computer Science Paper 2

Index out of range

An error caused by using an index below the lower bound or above the upper bound, so the element does not exist. Example: Marks[31] in ARRAY[1:30] or Marks[0] when the lower bound is 1.

Understanding Index out of range in IGCSE Computer Science Paper 2

What it does: This condition is true only when the index lies within the bounds 1 to 30, so the element exists. The array has an upper bound of 30, so the final loop value of 31 is out of range. The loop should end at 30. The lower bound is 1, so index 0 is below the lower bound and does not exist. Marks[30] is the last valid element. The array starts at index 1, so Names[0] is out of range. This is a common off-by-one mistake from counting from zero.

Common mistakes: When i is 30, Marks[i + 1] is Marks[31]. The upper bound is 30, so this index is out of range. It should be Marks[i]. Count is 10, so n reaches 10. Scores only goes up to index 9, so Scores[10] is out of range. The loop must end at Count - 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
Marks is declared as ARRAY[1:30] OF INTEGER. What happens when the program runs Marks[31] ← 50?
The array grows to 31 elements so the value is storedIndex out of range error: 31 is above the upper bound
Matches
DECLARE Scores : ARRAY[1:10] OF INTEGER Scores[10 + 1] <- 50
↓
The index 11 is above the upper bound of 10, so the element does not exist. Use an index from 1 to 10.
Applying
Meanings
Term
index out of range
Meaning
An error caused by using an index below the lower bound or above the upper bound, so the element does not exist. Example: Marks[31] in ARRAY[1:30] or Marks[0] when the lower bound is 1.
Practice
DECLARE Marks : ARRAY[1:30] OF INTEGER
Testing
Gaps
Marks[] ← 50
Check Answer
Spots
3FOR i ← 1 TO 30
4 Total ← Total + Marks[i + 1]

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

The 'Desert Tortoise' badge — Desert Master, 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. Marks is declared as ARRAY[1:30] OF INTEGER. What happens when the program runs Marks[31] ← 50?
A. Index out of range error: 31 is above the upper bound ✓
B. The array grows to 31 elements so the value is stored
An array has a fixed length. The upper bound is 30, so element 31 does not exist and using it is an error.
2. Marks is declared as ARRAY[1:30] OF INTEGER. Which statement uses an index that is out of range?
A. OUTPUT Marks[0] ✓
B. OUTPUT Marks[30]
The lower bound is 1, so index 0 is below the lower bound and does not exist. Marks[30] is the last valid element.
3. Names is declared as ARRAY[0:9] OF STRING. Which index is out of range?
A. Names[10] ✓
B. Names[9]
Bounds 0 to 9 give 10 elements. The highest valid index is 9, so Names[10] is above the upper bound.
4. Scores is declared as ARRAY[1:10] OF INTEGER. Which loop causes an off-by-one index error?
A. FOR i ← 1 TO 10
B. FOR i ← 1 TO 11 ✓
On the last pass i is 11, which is above the upper bound of 10, so Scores[i] does not exist.
5. Which check tests that an index is valid for ARRAY[1:30]?
A. IF Index >= 1 OR Index <= 30
B. IF Index >= 1 AND Index <= 30 ✓
A valid index must be at or above the lower bound AND at or below the upper bound. With OR, almost every number passes, including 0 and 31.

Match the Pairs

Marks[31] when Marks is ARRAY[1:30] → Index above the upper bound, so the element does not exist and the program gives an error
Marks[0] when Marks is ARRAY[1:30] → Index below the lower bound, so the element does not exist and the program gives an error
Check an index is at least the lower bound and at most the upper bound before using it → IF Index >= 1 AND Index <= 30 THEN
An off-by-one mistake: the loop goes one step past the upper bound of ARRAY[1:30] → FOR Count <- 1 TO 31
DECLARE Scores : ARRAY[1:10] OF INTEGER Scores[10 + 1] <- 50 → The index 11 is above the upper bound of 10, so the element does not exist. Use an index from 1 to 10.
DECLARE Names : ARRAY[1:5] OF STRING OUTPUT Names[0] → The index 0 is below the lower bound of 1, so the element does not exist. Use an index from 1 to 5.

Key Term

index out of range
An error caused by using an index below the lower bound or above the upper bound, so the element does not exist. Example: Marks[31] in ARRAY[1:30] or Marks[0] when the lower bound is 1.

Fill in the Blanks

1. DECLARE Marks : ARRAY[1:30] OF INTEGER Marks[31] ← 50
Hint: Type the first index just above the upper bound, which does not exist.
2. DECLARE Marks : ARRAY[1:30] OF INTEGER Marks[0] ← 50
Hint: Type the lower bound that makes index 0 fall below the start of the array.
3. IF Index >= 1 AND Index <= 30 THEN OUTPUT Marks[Index] ENDIF
Hint: Type the logical operator that needs both bound checks to be true before the index is used.
4. DECLARE Marks : ARRAY[1:30] OF INTEGER Marks[30] ← 100
Hint: Type the index of the last valid element, which is the upper bound.
5. IF Index < 1 OR Index > 30 THEN OUTPUT "Index out of range" ENDIF
Hint: Type the logical operator that flags an error if the index is either below or above the bounds.

Spot the Error

Question 1
A // Marks is ARRAY[1:30]
B Total ← 0
C FOR i ← 1 TO 30
D Total ← Total + Marks[i + 1]
E NEXT i
Line D — When i is 30, Marks[i + 1] is Marks[31]. The upper bound is 30, so this index is out of range. It should be Marks[i].
Question 2
A // Scores is ARRAY[0:9]
B Total ← 0
C Count ← 10
D FOR n ← 0 TO Count
E Total ← Total + Scores[n]
F NEXT n
Line D — Count is 10, so n reaches 10. Scores only goes up to index 9, so Scores[10] is out of range. The loop must end at Count - 1.
Question 3
A // Names is ARRAY[1:20]
B INPUT Pos
C IF Pos >= 1 AND Pos <= 21 THEN
D OUTPUT Names[Pos]
E ENDIF
Line C — The upper bound is 20, so the check must use <= 20. With <= 21, the index 21 passes the check and Names[21] is out of range.
Question 4
A // Marks is ARRAY[1:30]
B Marks[0] ← 55
C Marks[30] ← 62
D OUTPUT Marks[1]
Line B — The lower bound is 1, so Marks[0] does not exist and the index is out of range. The first element is Marks[1].
Question 5
A // Temp is ARRAY[1:7]
B Last ← 7
C OUTPUT Temp[Last + 1]
D OUTPUT Temp[1]
Line C — Last is 7, the upper bound. Temp[Last + 1] is Temp[8], which does not exist. This off-by-one mistake should be Temp[Last].

Student with a class code? Join your class →