← IGCSE Computer Science Paper 2

Writing several items to a file with a FOR loop

A FOR loop with INPUT and WRITEFILE inside repeats a fixed number of times, writing one item to the open file on each repeat. CLOSEFILE goes after NEXT.

Understanding Writing several items to a file with a FOR loop in IGCSE Computer Science Paper 2

What it does: OPENFILE comes before the FOR line, so the file is opened once and stays open for all five repeats. INPUT stores one name in the variable, then WRITEFILE saves it. This pair repeats for every name. CLOSEFILE runs once, after the loop has finished. If it is inside the loop, the file is closed after the first item. The value must be input first. Otherwise WRITEFILE would write a value that has not been entered yet. The FOR line sets how many times the INPUT and WRITEFILE lines run: here 5 times, once for each item.

Common mistakes: The program is meant to write the names to the file, so the loop needs WRITEFILE, not READFILE. Line 4 should be WRITEFILE "Names.txt", Name. OUTPUT only displays a value. It does not take a value from the user, so Mark would have no data to write. Line 3 should be INPUT Mark.

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 structure writes five scores to an open file?
FOR Count ← 1 TO 5 with INPUT Score and WRITEFILE inside the loopINPUT Score and WRITEFILE written once, with no loop
Matches
FOR Count ← 1 TO 5 INPUT Score WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt" NEXT Count
↓
CLOSEFILE is inside the loop, so the file is closed after the first score and the next WRITEFILE fails. It should go after NEXT Count.
Applying
Meanings
Term
FOR Count ← 1 TO 5
Meaning
A FOR loop with INPUT and WRITEFILE inside repeats a fixed number of times, writing one item to the open file on each repeat. CLOSEFILE goes after NEXT.
Practice
OPENFILE "Scores.txt" FOR WRITE
Testing
Gaps
OPENFILE "Scores.txt" FOR
Check Answer
Spots
3 INPUT Name
4 READFILE "Names.txt", Name

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 structure writes five scores to an open file?
A. FOR Count ← 1 TO 5 with INPUT Score and WRITEFILE inside the loop ✓
B. INPUT Score and WRITEFILE written once, with no loop
A FOR loop repeats the INPUT and WRITEFILE lines. Each repeat writes one item to the file.
2. Where does OPENFILE go when writing five names with a FOR loop?
A. Before the FOR loop ✓
B. Inside the FOR loop
Open the file once before the loop. Putting OPENFILE inside the loop would reopen the file on every repeat, and WRITE mode would replace the old data each time.
3. Where does CLOSEFILE go in a FOR loop that writes to a file?
A. Before NEXT, inside the loop
B. After NEXT ✓
CLOSEFILE runs once, after the loop has finished. If it is inside the loop, the file is closed after the first item.
4. Which order is correct inside the loop body?
A. WRITEFILE "names.txt", Name, then INPUT Name
B. INPUT Name, then WRITEFILE "names.txt", Name ✓
The value must be input first. Otherwise WRITEFILE would write a value that has not been entered yet.
5. How many items does this loop write to the file? FOR Count ← 1 TO 5 INPUT Score WRITEFILE "scores.txt", Score NEXT Count
A. 5 items, one on each repeat ✓
B. 1 item, because there is only one WRITEFILE line
The WRITEFILE line is inside the loop, so it runs five times. Each run writes one item.

Match the Pairs

FOR Count ← 1 TO 5 → Starts a loop that repeats its body exactly five times, so five items can be written to the file.
WRITEFILE "Scores.txt", Score → Writes the value just entered into the open file. Inside the loop, this happens on every repeat.
The file is opened for writing once, before the loop starts. → OPENFILE "Names.txt" FOR WRITE FOR Count ← 1 TO 5
Inside the loop, each item is input and then written to the file. → INPUT Name WRITEFILE "Names.txt", Name
FOR Count ← 1 TO 5 INPUT Score WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt" NEXT Count → CLOSEFILE is inside the loop, so the file is closed after the first score and the next WRITEFILE fails. It should go after NEXT Count.
FOR Count ← 1 TO 5 OPENFILE "Scores.txt" FOR WRITE INPUT Score WRITEFILE "Scores.txt", Score NEXT Count → OPENFILE is inside the loop. WRITE mode replaces the old data each time, so only the last score is kept. It should go before the loop.

Key Term

FOR Count ← 1 TO 5
A FOR loop with INPUT and WRITEFILE inside repeats a fixed number of times, writing one item to the open file on each repeat. CLOSEFILE goes after NEXT.

Fill in the Blanks

1. OPENFILE "Scores.txt" FOR WRITE FOR Count ← 1 TO 5 INPUT Score
Hint: Type the file mode used when saving new data into a file.
2. FOR Count ← 1 TO 5 INPUT Score WRITEFILE "Scores.txt", Score
Hint: Type the keyword that gets a value from the user on each repeat.
3. FOR Count ← 1 TO 5 INPUT Name WRITEFILE "Names.txt", Name
Hint: Type the command that writes the item into the open file.
4. WRITEFILE "Scores.txt", Score NEXT Count CLOSEFILE "Scores.txt"
Hint: Type the name of the loop counter variable that ends the loop.
5. WRITEFILE "Scores.txt", Score NEXT Count CLOSEFILE "Scores.txt"
Hint: Type the command that finishes with the file once the loop has ended.

Spot the Error

Question 1
A OPENFILE "Names.txt" FOR WRITE
B FOR Count ← 1 TO 5
C INPUT Name
D READFILE "Names.txt", Name
E NEXT Count
F CLOSEFILE "Names.txt"
Line D — The program is meant to write the names to the file, so the loop needs WRITEFILE, not READFILE. Line 4 should be WRITEFILE "Names.txt", Name.
Question 2
A OPENFILE "Marks.txt" FOR WRITE
B FOR Count ← 1 TO 5
C OUTPUT Mark
D WRITEFILE "Marks.txt", Mark
E NEXT Count
F CLOSEFILE "Marks.txt"
Line C — OUTPUT only displays a value. It does not take a value from the user, so Mark would have no data to write. Line 3 should be INPUT Mark.
Question 3
A OPENFILE "Names.txt" FOR WRITE
B FOR Count ← 1 TO 5
C INPUT "Name"
D WRITEFILE "Names.txt", Name
E NEXT Count
F CLOSEFILE "Names.txt"
Line C — INPUT must store the value in a variable. Putting the name in quotes makes it a string, not a variable. Line 3 should be INPUT Name.
Question 4
A OPENFILE "Marks.txt" FOR WRITE
B FOR Count ← 1 UNTIL 5
C INPUT Mark
D WRITEFILE "Marks.txt", Mark
E NEXT Count
F CLOSEFILE "Marks.txt"
Line B — A count-controlled FOR loop uses the keyword TO between the start and end values. UNTIL is not part of FOR. Line 2 should be FOR Count ← 1 TO 5.
Question 5
A OPENFILE "Marks.txt" FOR WRITE
B FOR Count ← 1 TO 5
C INPUT Marks
D WRITEFILE "Marks.txt", Mark
E NEXT Count
F CLOSEFILE "Marks.txt"
Line C — INPUT stores the value in Marks, but WRITEFILE writes the variable Mark, so the typed value is never written. The variable names must match. Line 3 should be INPUT Mark.

Student with a class code? Join your class →