← IGCSE Computer Science Paper 2

Reading several items from a file with a FOR loop

Repeats READFILE and OUTPUT 5 times to read 5 items from an open file, one per repeat, in the order written. Put CLOSEFILE after NEXT.

Understanding Reading several items from a file with a FOR loop in IGCSE Computer Science Paper 2

What it does: OPENFILE ... FOR READ comes before the FOR loop so the file is open for every repeat. Each READFILE moves on to the next item in the file, so the first repeat gets the first item written, the second repeat gets the second, and so on. You must read the item into the variable before you can output it. Otherwise OUTPUT would show an old or empty value. The file must stay open for all 5 repeats, so CLOSEFILE belongs after NEXT.

Common mistakes: The file is open for reading, so the loop must use READFILE to bring each item into Mark. WRITEFILE would try to put data into the file. Line 3 should be READFILE "Marks.txt", Mark. Quote marks make OUTPUT print the word Name instead of the data read from the file. The variable must not be in quotes: OUTPUT 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 489–499) and word search (Modules 479–499).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which program reads 5 items from an open file and displays each one?
FOR Count ← 1 TO 5 READFILE "Scores.txt", Item OUTPUT Item NEXT CountFOR Count ← 1 TO 5 OUTPUT Item NEXT Count
Matches
FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score CLOSEFILE "Scores.txt" NEXT Count
↓
CLOSEFILE is inside the loop, so the file is closed after the first repeat and the other items cannot be read. It should be after NEXT Count.
Applying
Meanings
Term
FOR Count ← 1 TO 5
Meaning
Repeats READFILE and OUTPUT 5 times to read 5 items from an open file, one per repeat, in the order written. Put CLOSEFILE after NEXT.
Practice
OPENFILE "Scores.txt" FOR READ
Testing
Gaps
OPENFILE "Scores.txt" FOR
Check Answer
Spots
2FOR Count ← 1 TO 5
3 WRITEFILE "Marks.txt", Mark

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

The 'Arctic Fox' badge — Tundra Tracker, 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 program reads 5 items from an open file and displays each one?
A. FOR Count ← 1 TO 5 READFILE "Scores.txt", Item OUTPUT Item NEXT Count ✓
B. FOR Count ← 1 TO 5 OUTPUT Item NEXT Count
The loop body needs READFILE to fetch a new item on each repeat, then OUTPUT to show it. OUTPUT alone would just show the same variable.
2. Where should OPENFILE ... FOR READ go when you read several items with a FOR loop?
A. Inside the loop, so the file is opened on every repeat
B. Before the FOR line, so the file is opened once ✓
Open the file once before the loop. Opening it inside the loop would be repeated on every pass and is wrong.
3. Where does CLOSEFILE belong?
A. After NEXT Count, once all items are read ✓
B. Inside the loop, just before NEXT Count
CLOSEFILE goes after NEXT so the file stays open until the loop has finished reading every item.
4. A file was written with the items 10, 20, 30. A FOR loop reads three items and outputs each. What is displayed?
A. 10, 20, 30 (the order they were written) ✓
B. 30, 20, 10 (the newest item first)
READFILE returns items in the same order they were written, starting from the first item.
5. How many items does FOR Count ← 1 TO 5 with READFILE inside read from the file?
A. 1 item, then it reads the whole file at once
B. 5 items, one on each repeat ✓
Each repeat of the loop runs READFILE once, so 5 repeats read 5 items.

Match the Pairs

FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score NEXT Count → Reads 5 items from the open file, one on each repeat, and displays each one as it is read
CLOSEFILE "Scores.txt" → Closes the file once the loop has finished, so it goes after NEXT and not inside the loop
Before the loop starts, the file must be opened for reading so READFILE has something to read from → OPENFILE "Scores.txt" FOR READ
Items come back in the order they were written, so each READFILE inside the loop gets the next item in the file → READFILE "Scores.txt", Score
FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score CLOSEFILE "Scores.txt" NEXT Count → CLOSEFILE is inside the loop, so the file is closed after the first repeat and the other items cannot be read. It should be after NEXT Count.
FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score NEXT Count CLOSEFILE "Scores.txt" → OPENFILE FOR READ is missing before the loop, so the file is never opened and READFILE cannot read from it. Add OPENFILE "Scores.txt" FOR READ first.

Key Term

FOR Count ← 1 TO 5
Repeats READFILE and OUTPUT 5 times to read 5 items from an open file, one per repeat, in the order written. Put CLOSEFILE after NEXT.

Fill in the Blanks

1. OPENFILE "Scores.txt" FOR READ FOR Count ← 1 TO 5 READFILE "Scores.txt", Item
Hint: Type the file mode used before the loop when the program wants to get data out of the file.
2. // The file holds 5 items FOR Count ← 1 TO 5 READFILE "Scores.txt", Item
Hint: Type the number of repeats that matches the number of items stored in the file.
3. FOR Count ← 1 TO 5 READFILE "Scores.txt", Item OUTPUT Item
Hint: Type the command inside the loop that takes the next item from the open file into a variable.
4. READFILE "Scores.txt", Item OUTPUT Item NEXT Count
Hint: Type the command that displays each item on the screen after it has been read.
5. OUTPUT Item NEXT Count CLOSEFILE "Scores.txt"
Hint: Type the command that goes after the loop has finished to finish using the file.

Spot the Error

Question 1
A OPENFILE "Marks.txt" FOR READ
B FOR Count ← 1 TO 5
C WRITEFILE "Marks.txt", Mark
D OUTPUT Mark
E NEXT Count
F CLOSEFILE "Marks.txt"
Line C — The file is open for reading, so the loop must use READFILE to bring each item into Mark. WRITEFILE would try to put data into the file. Line 3 should be READFILE "Marks.txt", Mark.
Question 2
A OPENFILE "Names.txt" FOR READ
B FOR Count ← 1 TO 4
C READFILE "Names.txt", Name
D OUTPUT "Name"
E NEXT Count
F CLOSEFILE "Names.txt"
Line D — Quote marks make OUTPUT print the word Name instead of the data read from the file. The variable must not be in quotes: OUTPUT Name.
Question 3
A OPENFILE "Temps.txt" FOR READ
B FOR Count ← 1 TO 7
C READFILE "Temps.txt", Temp
D OUTPUT Temps
E NEXT Count
F CLOSEFILE "Temps.txt"
Line D — READFILE stores each item in the variable Temp, but OUTPUT uses a different name, Temps, which holds nothing. The variable names must match: OUTPUT Temp.
Question 4
A OPENFILE "Prices.txt" FOR READ
B FOR Count ← 1 TILL 5
C READFILE "Prices.txt", Price
D OUTPUT Price
E NEXT Count
F CLOSEFILE "Prices.txt"
Line B — A count-controlled loop uses the keyword TO between the start and end values, not TILL. Line 2 should be FOR Count ← 1 TO 5.
Question 5
A OPENFILE "Items.txt" FOR READ
B FOR Count ← 5 TO 1
C READFILE "Items.txt", Item
D OUTPUT Item
E NEXT Count
F CLOSEFILE "Items.txt"
Line B — The loop must count up from 1 to 5 so it repeats 5 times, once for each item in the file. Starting at 5 and going TO 1 does not repeat correctly. It should be FOR Count ← 1 TO 5.

Student with a class code? Join your class →