Looping through an array with FOR (7d)

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

Looping through an array with FOR (7d)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop header visits every element of a 10-item array Marks, starting at index 1?
A FOR Index ← 1 TO 10
B FOR Index ← 0 TO 10
2. Inside FOR Index ← 1 TO 10, which line outputs the current element of Marks?
A OUTPUT Marks[10]
B OUTPUT Marks[Index]
3. Which statement about the loop FOR Index ← 1 TO 10 is true?
A Index is the value stored in the array
B Index is the counter and also the position in the array
4. Which loop fills an array with 10 marks typed by the user?
A FOR Index ← 1 TO 10 INPUT Marks[Index] NEXT Index
B FOR Index ← 1 TO 10 INPUT Marks NEXT Index
5. How many times does the body of FOR Index ← 1 TO 10 run?
A 10 times
B 9 times

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 10 ARepeats the loop body once for each array position from 1 to 10, with Index counting up each time
2OUTPUT Marks[Index] BThe loop starts at 0 and runs 11 times, but the array has positions 1 to 10, so Marks[0] does not exist.
3Fill an array by asking the user for a value at each position as the counter moves along CDisplays the element at the current position, so each pass of the loop shows the next mark
4Output every element of the array in order using the loop counter as the index DFOR Index ← 1 TO 10 INPUT Marks[Index] NEXT Index
5FOR Index ← 1 TO 10 OUTPUT Marks[10] NEXT Index EFOR Index ← 1 TO 10 OUTPUT Marks[Index] NEXT Index
6FOR Index ← 0 TO 10 INPUT Marks[Index] NEXT Index FThe index is fixed at 10, so the same element is output ten times instead of using Index to visit each element.

Definitions

FOR Index ← 1 TO 10

Fill in the Blanks

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

1.
FOR Index ← 1 __ 10 INPUT Marks[Index] NEXT Index
2.
FOR Index ← 1 TO 10 ______ Marks[Index] NEXT Index
3.
FOR Index ← 1 TO 10 INPUT Marks[_____] NEXT Index
4.
DECLARE Marks : ARRAY[1:10] OF INTEGER FOR Index ← 1 TO __ INPUT Marks[Index]
5.
FOR Index ← 1 TO 10 OUTPUT Marks[Index] ____ Index

Spot the Error

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

Question 1
A FOR Index = 1 TO 10
B INPUT Marks[Index]
C NEXT Index
Question 2
A FOR Index ← 1 UNTIL 10
B OUTPUT Marks[Index]
C NEXT Index
Question 3
A Sum ← 0
B FOR Index ← 1 TO 10
C Sum ← Sum + Marks[1]
D NEXT Index
Question 4
A FOR Index ← 1 TO 10
B OUTPUT "Enter mark"
C INPUT Marks[Index]
D ENDFOR
Question 5
A FOR Index ← 1 TO 10
B OUTPUT Marks(Index)
C NEXT Index

Answer Key

Looping through an array with FOR (7d)
Section A: Quick Questions
Q1
A — FOR Index ← 1 TO 10
Q2
B — OUTPUT Marks[Index]
Q3
B — Index is the counter and also the position in the array
Q4
A — FOR Index ← 1 TO 10 INPUT Marks[Index] NEXT Index
Q5
A — 10 times
Section B: Match the Pairs
Q1
FOR Index ← 1 TO 10 → Repeats the loop body once for each array position from 1 to 10, with Index counting up each time
Q2
OUTPUT Marks[Index] → Displays the element at the current position, so each pass of the loop shows the next mark
Q3
Fill an array by asking the user for a value at each position as the counter moves along → FOR Index ← 1 TO 10 INPUT Marks[Index] NEXT Index
Q4
Output every element of the array in order using the loop counter as the index → FOR Index ← 1 TO 10 OUTPUT Marks[Index] NEXT Index
Q5
FOR Index ← 1 TO 10 OUTPUT Marks[10] NEXT Index → The index is fixed at 10, so the same element is output ten times instead of using Index to visit each element.
Q6
FOR Index ← 0 TO 10 INPUT Marks[Index] NEXT Index → The loop starts at 0 and runs 11 times, but the array has positions 1 to 10, so Marks[0] does not exist.
Section C: Definitions
Q1
FOR Index ← 1 TO 10 — A loop whose counter is the array index. Inside it, Marks[Index] picks each element in turn, so the loop visits every item from position 1 to 10 once.
Section D: Fill in the Blanks
Q1
FOR Index ← 1 TO 10 INPUT Marks[Index] NEXT Index
Q2
FOR Index ← 1 TO 10 OUTPUT Marks[Index] NEXT Index
Q3
FOR Index ← 1 TO 10 INPUT Marks[Index] NEXT Index
Q4
DECLARE Marks : ARRAY[1:10] OF INTEGER FOR Index ← 1 TO 10 INPUT Marks[Index]
Q5
FOR Index ← 1 TO 10 OUTPUT Marks[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 10.
Q2
A — The end value of a FOR loop follows the keyword TO, not UNTIL. UNTIL belongs to REPEAT loops. It should be FOR Index ← 1 TO 10.
Q3
C — Marks[1] always picks the first element, so every pass adds the same mark. Use the loop counter: Marks[Index] so each element is visited in turn.
Q4
D — In Cambridge pseudocode a FOR loop ends with NEXT and the counter name, not ENDFOR. It should be NEXT Index.
Q5
B — Array elements are picked with square brackets, not round brackets. It should be OUTPUT Marks[Index].