← IGCSE Computer Science Paper 2

Looping through an array with FOR (7c)

Uses the loop counter Index as the array index, so Names[Index] visits every element in turn. Each one can be read, changed, input, output or totalled.

Understanding Looping through an array with FOR (7c) in IGCSE Computer Science Paper 2

What it does: INPUT Names[Index] stores each entered value in the element at the current counter value, so all 30 elements are filled. Names[1] never changes, so it would output the first name 30 times. Names[Index] moves along the array. Index is only the position number. To total the marks you must add the element Marks[Index]. The loop must stop at the last element. Going up to 30 would try to use Scores[21] and beyond, which do not exist.

Common mistakes: A FOR loop sets its counter with the assignment arrow ←, not =. It should be FOR Index ← 1 TO 30. NEXT must name the loop counter that the FOR line uses. It should be NEXT Index, not the array name.

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 176–187) and word search (Modules 166–187).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Names is an array with 30 elements, numbered 1 to 30. Which loop header visits every element once?
FOR Index ← 1 TO 30FOR Index ← 0 TO 30
Matches
FOR Index ← 1 TO 30 OUTPUT Names[30] NEXT Index
↓
The index is fixed at 30 instead of using the counter Index, so the loop outputs the last element 30 times and never visits the others.
Applying
Meanings
Term
FOR Index ← 1 TO 30
Meaning
Uses the loop counter Index as the array index, so Names[Index] visits every element in turn. Each one can be read, changed, input, output or totalled.
Practice
FOR Index ← 1 TO 30
Testing
Gaps
Names[Index] ""
Check Answer
Spots
1FOR Index = 1 TO 30
2 INPUT Names[Index]

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

The 'Sand Cat' badge — Precision Prowler, 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. Names is an array with 30 elements, numbered 1 to 30. Which loop header visits every element once?
A. FOR Index ← 1 TO 30 ✓
B. FOR Index ← 0 TO 30
The array runs from 1 to 30, so the counter must start at 1 and stop at 30. There is no element Names[0].
2. Which line inside the loop sets EVERY element of Names to an empty string?
A. Names[Index] ← "" ✓
B. Names[30] ← ""
Index changes on each pass, so Names[Index] is a different element each time. Names[30] only changes the last element, every time.
3. Which line inside the loop stores a typed name in each element of Names?
A. INPUT Names
B. INPUT Names[Index] ✓
INPUT needs one element to store the value. Names[Index] picks the next element on each pass of the loop.
4. Which loop outputs all 30 names, one after another?
A. FOR Index ← 1 TO 30 OUTPUT Names[1] NEXT Index
B. FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index ✓
Names[1] never changes, so it would output the first name 30 times. Names[Index] moves along the array.
5. Marks has 30 elements. Which line adds each mark to Total as the loop runs?
A. Total ← Total + Marks[Index] ✓
B. Total ← Total + Index
Index is only the position number. To total the marks you must add the element Marks[Index].

Match the Pairs

FOR Index ← 1 TO 30 Names[Index] ← "" NEXT Index → Sets every element of the Names array to an empty string, one element at a time
FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index → Displays every element of the Names array in turn, from element 1 to element 30
A FOR loop can store a value typed by the user in every element of an array → FOR Index ← 1 TO 30 INPUT Names[Index] NEXT Index
A FOR loop can add up every element of an array by using the counter as the index → FOR Index ← 1 TO 30 Total ← Total + Marks[Index] NEXT Index
FOR Index ← 1 TO 30 OUTPUT Names[30] NEXT Index → The index is fixed at 30 instead of using the counter Index, so the loop outputs the last element 30 times and never visits the others.
FOR Index ← 0 TO 30 INPUT Names[Index] NEXT Index → The loop bounds do not match the array. The array runs from 1 to 30, so starting at 0 refers to an element that does not exist and the loop runs 31 times.

Key Term

FOR Index ← 1 TO 30
Uses the loop counter Index as the array index, so Names[Index] visits every element in turn. Each one can be read, changed, input, output or totalled.

Fill in the Blanks

1. FOR Index ← 1 TO 30 Names[Index] ← "" NEXT Index
Hint: Type the assignment symbol that sets each element to an empty string.
2. FOR Index ← 1 TO 30 INPUT Names[Index] NEXT Index
Hint: Type the loop counter that is used as the array position.
3. FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index
Hint: Type the keyword that separates the start value from the end value of the loop.
4. FOR Index ← 1 TO 30 Total ← Total + Scores[Index] NEXT Index
Hint: Type the arithmetic operator that adds each element to the running total.
5. FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index
Hint: Type the keyword that ends the loop body and moves the counter on to the next value.

Spot the Error

Question 1
A FOR Index = 1 TO 30
B INPUT Names[Index]
C NEXT Index
Line A — A FOR loop sets its counter with the assignment arrow ←, not =. It should be FOR Index ← 1 TO 30.
Question 2
A DECLARE Names : ARRAY[1:30] OF STRING
B FOR Index ← 1 TO 30
C Names[Index] ← ""
D NEXT Names
Line D — NEXT must name the loop counter that the FOR line uses. It should be NEXT Index, not the array name.
Question 3
A DECLARE Sum : INTEGER
B Sum ← 1
C FOR Index ← 1 TO 30
D Sum ← Sum + Marks[Index]
E NEXT Index
Line B — The total must start at 0 before the loop. Starting at 1 makes the final answer one too big. It should be Sum ← 0.
Question 4
A FOR Index ← 0 TO 30
B OUTPUT Names[Index]
C NEXT Index
Line A — The array runs from index 1 to 30, so there is no element Names[0]. The loop must start at 1: FOR Index ← 1 TO 30.
Question 5
A FOR Index ← 1 TO 30
B INPUT Names
C OUTPUT Names[Index]
D NEXT Index
Line B — Without [Index] the loop does not pick out one element. To store each input in its own element, use INPUT Names[Index].

Student with a class code? Join your class →