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.
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 →