Looping through an array with FOR (7c)

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

Looping through an array with FOR (7c)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Names is an array with 30 elements, numbered 1 to 30. Which loop header visits every element once?
A FOR Index ← 1 TO 30
B FOR Index ← 0 TO 30
2. Which line inside the loop sets EVERY element of Names to an empty string?
A Names[Index] ← ""
B Names[30] ← ""
3. Which line inside the loop stores a typed name in each element of Names?
A INPUT Names
B INPUT Names[Index]
4. Which loop outputs all 30 names, one after another?
A FOR Index ← 1 TO 30 OUTPUT Names[1] NEXT Index
B FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index
5. Marks has 30 elements. Which line adds each mark to Total as the loop runs?
A Total ← Total + Marks[Index]
B Total ← Total + Index

Match the Pairs

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

Left columnRight column
1FOR Index ← 1 TO 30 Names[Index] ← "" NEXT Index ADisplays every element of the Names array in turn, from element 1 to element 30
2FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index BSets every element of the Names array to an empty string, one element at a time
3A FOR loop can store a value typed by the user in every element of an array CThe loop bounds do not match the array. The array runs from 1 to 30, so starting at 0 refers to an element that does not exist and the loop runs 31 times.
4A FOR loop can add up every element of an array by using the counter as the index DFOR Index ← 1 TO 30 INPUT Names[Index] NEXT Index
5FOR Index ← 1 TO 30 OUTPUT Names[30] NEXT Index EThe index is fixed at 30 instead of using the counter Index, so the loop outputs the last element 30 times and never visits the others.
6FOR Index ← 0 TO 30 INPUT Names[Index] NEXT Index FFOR Index ← 1 TO 30 Total ← Total + Marks[Index] NEXT Index

Definitions

FOR Index ← 1 TO 30

Fill in the Blanks

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

1.
FOR Index ← 1 TO 30 Names[Index] _ "" NEXT Index
2.
FOR Index ← 1 TO 30 INPUT Names[_____] NEXT Index
3.
FOR Index ← 1 __ 30 OUTPUT Names[Index] NEXT Index
4.
FOR Index ← 1 TO 30 Total ← Total _ Scores[Index] NEXT Index
5.
FOR Index ← 1 TO 30 OUTPUT Names[Index] ____ Index

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A FOR Index = 1 TO 30
B INPUT Names[Index]
C NEXT Index
Question 2
A DECLARE Names : ARRAY[1:30] OF STRING
B FOR Index ← 1 TO 30
C Names[Index] ← ""
D NEXT Names
Question 3
A DECLARE Sum : INTEGER
B Sum ← 1
C FOR Index ← 1 TO 30
D Sum ← Sum + Marks[Index]
E NEXT Index
Question 4
A FOR Index ← 0 TO 30
B OUTPUT Names[Index]
C NEXT Index
Question 5
A FOR Index ← 1 TO 30
B INPUT Names
C OUTPUT Names[Index]
D NEXT Index

Answer Key

Looping through an array with FOR (7c)
Section A: Quick Questions
Q1
A — FOR Index ← 1 TO 30
Q2
A — Names[Index] ← ""
Q3
B — INPUT Names[Index]
Q4
B — FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index
Q5
A — Total ← Total + Marks[Index]
Section B: Match the Pairs
Q1
FOR Index ← 1 TO 30 Names[Index] ← "" NEXT Index → Sets every element of the Names array to an empty string, one element at a time
Q2
FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index → Displays every element of the Names array in turn, from element 1 to element 30
Q3
A FOR loop can store a value typed by the user in every element of an array → FOR Index ← 1 TO 30 INPUT Names[Index] NEXT Index
Q4
A FOR loop can add up every element of an array by using the counter as the index → FOR Index ← 1 TO 30 Total ← Total + Marks[Index] NEXT Index
Q5
FOR Index ← 1 TO 30 OUTPUT Names[30] NEXT Index → The index is fixed at 30 instead of using the counter Index, so the loop outputs the last element 30 times and never visits the others.
Q6
FOR Index ← 0 TO 30 INPUT Names[Index] NEXT Index → The loop bounds do not match the array. The array runs from 1 to 30, so starting at 0 refers to an element that does not exist and the loop runs 31 times.
Section C: Definitions
Q1
FOR Index ← 1 TO 30 — Uses the loop counter Index as the array index, so Names[Index] visits every element in turn. Each one can be read, changed, input, output or totalled.
Section D: Fill in the Blanks
Q1
FOR Index ← 1 TO 30 Names[Index] ← "" NEXT Index
Q2
FOR Index ← 1 TO 30 INPUT Names[Index] NEXT Index
Q3
FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index
Q4
FOR Index ← 1 TO 30 Total ← Total + Scores[Index] NEXT Index
Q5
FOR Index ← 1 TO 30 OUTPUT Names[Index] NEXT Index
Section E: Spot the Error
Q1
A — A FOR loop sets its counter with the assignment arrow ←, not =. It should be FOR Index ← 1 TO 30.
Q2
D — NEXT must name the loop counter that the FOR line uses. It should be NEXT Index, not the array name.
Q3
B — The total must start at 0 before the loop. Starting at 1 makes the final answer one too big. It should be Sum ← 0.
Q4
A — The array runs from index 1 to 30, so there is no element Names[0]. The loop must start at 1: FOR Index ← 1 TO 30.
Q5
B — Without [Index] the loop does not pick out one element. To store each input in its own element, use INPUT Names[Index].