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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
FOR Count ← 1 TO 5Fill in the Blanks
Spot the Error
Student with a class code? Join your class →