← IGCSE Computer Science Paper 2

Setting every element with a FOR loop

A loop that uses its counter as the array index, so each pass assigns a value to one element, e.g. Scores[Index] ← 0. The loop limits must match the array bounds.

Understanding Setting every element with a FOR loop in IGCSE Computer Science Paper 2

What it does: The loop starts at the lower bound 1 and finishes at the upper bound 30, so every element is reached and none is out of range. Each pass of the loop assigns 0 to Scores[Index], so after 30 passes all 30 elements hold 0. The loop limits must match the array bounds 1 and 10. Going to 11 would try to write to Scores[11], which is out of range. The counter picks which element to set. The value assigned (such as 0 or "") is written separately on the right of the arrow. Scores[Index] ← 0 stores 0 in every element. Scores[Index] ← Index would store 1, 2, 3 and so on instead of setting all of them to 0.

Common mistakes: NEXT must name the loop variable that FOR created. It should be NEXT Index, not the array name. A FOR loop counts upwards unless a STEP is given, so 30 TO 1 never runs and no elements are set. It should be FOR Index ← 1 TO 30.

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
Which loop sets every element of StudentNames (ARRAY[1:30]) to an empty string?
FOR Index ← 1 TO 30 StudentNames[Index] ← "" NEXT IndexFOR Index ← 1 TO 30 StudentNames ← "" NEXT Index
Matches
FOR Index ← 1 TO 31 Scores[Index] ← 0 NEXT Index
↓
The loop limit 31 is larger than the upper bound 30 of the array, so the last pass writes to an index that does not exist and causes an index out of range error. It should be TO 30.
Applying
Meanings
Term
FOR Index ← 1 TO 30
Meaning
A loop that uses its counter as the array index, so each pass assigns a value to one element, e.g. Scores[Index] ← 0. The loop limits must match the array bounds.
Practice
DECLARE Scores : ARRAY[1:30] OF INTEGER
Testing
Gaps
FOR Index ← 1 30
Check Answer
Spots
3 Scores[Index] ← 0
4NEXT Scores

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

The 'Giant Isopod' badge — Patient Builder, 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 loop sets every element of StudentNames (ARRAY[1:30]) to an empty string?
A. FOR Index ← 1 TO 30 StudentNames[Index] ← "" NEXT Index ✓
B. FOR Index ← 1 TO 30 StudentNames ← "" NEXT Index
The loop variable must be used as the index, StudentNames[Index], so each pass sets one element. Without the index you are not assigning to an element.
2. Scores is declared as ARRAY[1:10] OF INTEGER. Which loop limits set every element to 0?
A. FOR Index ← 1 TO 11
B. FOR Index ← 1 TO 10 ✓
The loop limits must match the array bounds 1 and 10. Going to 11 would try to write to Scores[11], which is out of range.
3. What does this code do? FOR Index ← 1 TO 5 Scores[Index] ← 0 NEXT Index
A. Sets Scores[1] to Scores[5] to 0, one element per pass ✓
B. Sets only Scores[5] to 0 after looping five times
Index changes on every pass (1, 2, 3, 4, 5), so each pass assigns 0 to a different element.
4. Marks is declared as ARRAY[0:9] OF INTEGER. Which loop sets all the elements to 0?
A. FOR Index ← 1 TO 10
B. FOR Index ← 0 TO 9 ✓
The loop must start at the lower bound 0 and stop at the upper bound 9. Starting at 1 misses Marks[0], and going to 10 is out of range.
5. Which statement about setting every element with a FOR loop is correct?
A. The loop variable is used as the array index ✓
B. The loop variable is used as the value stored
The counter picks which element to set. The value assigned (such as 0 or "") is written separately on the right of the arrow.

Match the Pairs

FOR Index ← 1 TO 30 StudentNames[Index] ← "" NEXT Index → Sets all 30 elements of StudentNames to an empty string, one element on each pass of the loop
Scores[Index] ← 0 → Assigns 0 to the element at the position held in Index; inside a FOR loop this resets one element per pass
The loop limits must match the array bounds, so an array declared as ARRAY[1:30] is filled by a loop from 1 to 30 → FOR Index ← 1 TO 30
A FOR loop can set every element of a Scores array to 0 by using the loop variable as the index → FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
FOR Index ← 1 TO 31 Scores[Index] ← 0 NEXT Index → The loop limit 31 is larger than the upper bound 30 of the array, so the last pass writes to an index that does not exist and causes an index out of range error. It should be TO 30.
FOR Index ← 1 TO 30 Scores[1] ← 0 NEXT Index → The fixed index 1 means only the first element is set, thirty times. It should use the loop variable: Scores[Index] ← 0.

Key Term

FOR Index ← 1 TO 30
A loop that uses its counter as the array index, so each pass assigns a value to one element, e.g. Scores[Index] ← 0. The loop limits must match the array bounds.

Fill in the Blanks

1. FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Hint: Type the keyword that separates the loop's start value from its end value.
2. FOR Index ← 1 TO 30 StudentNames[Index] ← "" NEXT Index
Hint: Type the loop variable that selects which element is assigned on each pass.
3. // Scores : ARRAY[0:9] OF INTEGER FOR Index ← 0 TO 9 Scores[Index] ← 0
Hint: Type the end value that matches the array's upper bound in the comment.
4. FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Hint: Type the keyword that marks the end of the loop body and moves to the next index.
5. FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Hint: Type the pseudocode symbol that assigns a value to the array element.

Spot the Error

Question 1
A // Scores is ARRAY[1:3]
B FOR Index ← 1 TO 3
C Scores[Index] ← 0
D NEXT Scores
Line D — NEXT must name the loop variable that FOR created. It should be NEXT Index, not the array name.
Question 2
A FOR Index ← 30 TO 1
B Scores[Index] ← 0
C NEXT Index
Line A — A FOR loop counts upwards unless a STEP is given, so 30 TO 1 never runs and no elements are set. It should be FOR Index ← 1 TO 30.
Question 3
A FOR Index 1 TO 30
B StudentNames[Index] ← ""
C NEXT Index
Line A — The FOR line needs the assignment arrow to give the loop variable its starting value. It should be FOR Index ← 1 TO 30.
Question 4
A // Scores is ARRAY[1:30]
B FOR Index ← 1 TO 31
C Scores[Index] ← 0
D NEXT Index
Line B — The upper bound of Scores is 30, so the loop must stop at 30. Going to 31 tries to write to Scores[31], which is out of range.
Question 5
A // Names is ARRAY[0:9]
B FOR Index ← 1 TO 9
C Names[Index] ← ""
D NEXT Index
Line B — The lower bound of Names is 0, so the loop must start at 0. Starting at 1 leaves Names[0] unset. It should be FOR Index ← 0 TO 9.

Student with a class code? Join your class →