Writing several items to a file with a FOR loop

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Writing several items to a file with a FOR loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Where does OPENFILE go when writing five names with a FOR loop?
A Before the FOR loop
B Inside the FOR loop
3. Where does CLOSEFILE go in a FOR loop that writes to a file?
A Before NEXT, inside the loop
B After NEXT
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1FOR Count ← 1 TO 5 AOPENFILE 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.
2WRITEFILE "Scores.txt", Score BStarts a loop that repeats its body exactly five times, so five items can be written to the file.
3The file is opened for writing once, before the loop starts. CINPUT Name WRITEFILE "Names.txt", Name
4Inside the loop, each item is input and then written to the file. DWrites the value just entered into the open file. Inside the loop, this happens on every repeat.
5FOR Count ← 1 TO 5 INPUT Score WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt" NEXT Count EOPENFILE "Names.txt" FOR WRITE FOR Count ← 1 TO 5
6FOR Count ← 1 TO 5 OPENFILE "Scores.txt" FOR WRITE INPUT Score WRITEFILE "Scores.txt", Score NEXT Count FCLOSEFILE is inside the loop, so the file is closed after the first score and the next WRITEFILE fails. It should go after NEXT Count.

Definitions

FOR Count ← 1 TO 5

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
OPENFILE "Scores.txt" FOR _____ FOR Count ← 1 TO 5 INPUT Score
2.
FOR Count ← 1 TO 5 _____ Score WRITEFILE "Scores.txt", Score
3.
FOR Count ← 1 TO 5 INPUT Name _________ "Names.txt", Name
4.
WRITEFILE "Scores.txt", Score NEXT _____ CLOSEFILE "Scores.txt"
5.
WRITEFILE "Scores.txt", Score NEXT Count _________ "Scores.txt"

Spot the Error

Shade in the letter of the line that contains an 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"
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"
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"
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"
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"

Answer Key

Writing several items to a file with a FOR loop
Section A: Quick Questions
Q1
A — FOR Count ← 1 TO 5 with INPUT Score and WRITEFILE inside the loop
Q2
A — Before the FOR loop
Q3
B — After NEXT
Q4
B — INPUT Name, then WRITEFILE "names.txt", Name
Q5
A — 5 items, one on each repeat
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 5 → Starts a loop that repeats its body exactly five times, so five items can be written to the file.
Q2
WRITEFILE "Scores.txt", Score → Writes the value just entered into the open file. Inside the loop, this happens on every repeat.
Q3
The file is opened for writing once, before the loop starts. → OPENFILE "Names.txt" FOR WRITE FOR Count ← 1 TO 5
Q4
Inside the loop, each item is input and then written to the file. → INPUT Name WRITEFILE "Names.txt", Name
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
OPENFILE "Scores.txt" FOR WRITE FOR Count ← 1 TO 5 INPUT Score
Q2
FOR Count ← 1 TO 5 INPUT Score WRITEFILE "Scores.txt", Score
Q3
FOR Count ← 1 TO 5 INPUT Name WRITEFILE "Names.txt", Name
Q4
WRITEFILE "Scores.txt", Score NEXT Count CLOSEFILE "Scores.txt"
Q5
WRITEFILE "Scores.txt", Score NEXT Count CLOSEFILE "Scores.txt"
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
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.