← IGCSE Computer Science Paper 2

Displaying every element with OUTPUT in a loop

Inside a FOR loop, this line displays every element of the array in order. Index changes on each pass, so each pass outputs the next element, from the lower bound to the upper bound.

Understanding Displaying every element with OUTPUT in a loop in IGCSE Computer Science Paper 2

What it does: Counting from the upper bound down to the lower bound makes Index visit the elements in reverse, so the last element is output first. Index changes each pass, so StudentNames[Index] outputs the next element each time. Using just the array name does not select one element. Text goes in quotes, but Index and Marks[Index] must be outside the quotes so their values are output. Several values are separated by commas. To count down you need STEP -1. Without STEP, the loop counts up and 10 TO 1 would not run at all.

Common mistakes: NEXT must name the same loop variable as the FOR line. The loop counts with Index, so the last line should be NEXT Index. A fixed index of 1 outputs the first element on every pass. The loop variable must be the index, so the line should be OUTPUT Marks[I].

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 459–468) and word search (Modules 459–478).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which loop body displays every element of the StudentNames array in order?
OUTPUT StudentNames[Index]OUTPUT StudentNames
Matches
FOR Index ← 1 TO 5 OUTPUT StudentNames NEXT Index
↓
The array name is used without an index, so it does not output one element per pass. It should be StudentNames[Index].
Applying
Meanings
Term
OUTPUT StudentNames[Index]
Meaning
Inside a FOR loop, this line displays every element of the array in order. Index changes on each pass, so each pass outputs the next element, from the lower bound to the upper bound.
Practice
FOR Index ← 1 TO 5
Testing
Gaps
OUTPUT [Index]
Check Answer
Spots
3 OUTPUT StudentNames[Index]
4NEXT Count

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

The 'Cosmic Dragon' badge — Orbit 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. Which loop body displays every element of the StudentNames array in order?
A. OUTPUT StudentNames[Index] ✓
B. OUTPUT StudentNames
Index changes each pass, so StudentNames[Index] outputs the next element each time. Using just the array name does not select one element.
2. StudentNames is declared ARRAY[1:30]. Which loop header visits every element?
A. FOR Index ← 0 TO 30
B. FOR Index ← 1 TO 30 ✓
The loop must run from the lower bound (1) to the upper bound (30). Starting at 0 would be an index out of range error.
3. True or false: OUTPUT StudentNames[Index] inside a FOR loop outputs a different element on each pass.
A. False
B. True ✓
The value of Index changes on every pass, so each pass outputs the next element of the array.
4. Which line shows the index and the mark together, like Mark 3: 72?
A. OUTPUT "Mark ", Index, ": ", Marks[Index] ✓
B. OUTPUT "Mark Index: Marks[Index]"
Text goes in quotes, but Index and Marks[Index] must be outside the quotes so their values are output. Several values are separated by commas.
5. Which loop header outputs the elements of Marks[1:10] in reverse order?
A. FOR Index ← 10 TO 1 STEP -1 ✓
B. FOR Index ← 10 TO 1
To count down you need STEP -1. Without STEP, the loop counts up and 10 TO 1 would not run at all.

Match the Pairs

OUTPUT StudentNames[Index] → Displays the element at the current index. Inside a FOR loop it outputs every element in order.
OUTPUT "Mark ", Index, ": ", Marks[Index] → Displays a label, the index number and the element, all on one line, separated by commas.
To display every element of an array in reverse order, the loop counts down using STEP -1. → FOR Index ← 10 TO 1 STEP -1 OUTPUT Marks[Index] NEXT Index
To display every element of a 5-element array in order, loop from the lower bound to the upper bound and output Names[Index]. → FOR Index ← 1 TO 5 OUTPUT Names[Index] NEXT Index
FOR Index ← 1 TO 5 OUTPUT StudentNames NEXT Index → The array name is used without an index, so it does not output one element per pass. It should be StudentNames[Index].
FOR Index ← 1 TO 5 OUTPUT Marks[5] NEXT Index → A fixed index of 5 is used instead of the loop variable, so the same element is output every time. It should be Marks[Index].

Key Term

OUTPUT StudentNames[Index]
Inside a FOR loop, this line displays every element of the array in order. Index changes on each pass, so each pass outputs the next element, from the lower bound to the upper bound.

Fill in the Blanks

1. FOR Index ← 1 TO 5 OUTPUT StudentNames[Index] NEXT Index
Hint: Type the name of the array whose elements are being displayed.
2. FOR Index ← 1 TO 5 OUTPUT StudentNames[Index] NEXT Index
Hint: Type the loop counter that picks the next element on each pass.
3. FOR Index ← 1 TO 10 OUTPUT "Mark ", Index, ": ", Marks[Index] NEXT Index
Hint: Type the array name that supplies the element shown after the text.
4. FOR Index ← 5 TO 1 STEP -1 OUTPUT StudentNames[Index] NEXT Index
Hint: Type the keyword that lets the loop count down so the output is in reverse.
5. FOR Index ← 1 TO 5 OUTPUT StudentNames[Index], Marks[Index] NEXT Index
Hint: Type the command that displays several comma-separated values on each pass.

Spot the Error

Question 1
A OUTPUT "Class list"
B FOR Index ← 1 TO 5
C OUTPUT StudentNames[Index]
D NEXT Count
Line D — NEXT must name the same loop variable as the FOR line. The loop counts with Index, so the last line should be NEXT Index.
Question 2
A OUTPUT "Marks"
B FOR I ← 1 TO 4
C OUTPUT "Mark number ", I
D OUTPUT Marks[1]
E NEXT I
Line D — A fixed index of 1 outputs the first element on every pass. The loop variable must be the index, so the line should be OUTPUT Marks[I].
Question 3
A FOR Index 1 TO 5
B OUTPUT Pets[Index]
C NEXT Index
Line A — The FOR line needs the assignment arrow to give the counter its starting value. It should be FOR Index ← 1 TO 5.
Question 4
A OUTPUT "Players"
B FOR Index ← 0 TO 3
C OUTPUT "Players[Index]"
D NEXT Index
Line C — Quote marks make OUTPUT display the text Players[Index] literally. Without quotes it outputs the element, so it should be OUTPUT Players[Index].
Question 5
A FOR Index ← 1 TO 6
B OUTPUT Colours(Index)
C NEXT Index
Line B — Array elements are accessed with square brackets, not round brackets. The line should be OUTPUT Colours[Index].

Student with a class code? Join your class →