Setting every element with a FOR loop

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

Setting every element with a FOR loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop sets every element of StudentNames (ARRAY[1:30]) to an empty string?
A FOR Index ← 1 TO 30 StudentNames[Index] ← "" NEXT Index
B FOR Index ← 1 TO 30 StudentNames ← "" NEXT Index
2. Scores is declared as ARRAY[1:10] OF INTEGER. Which loop limits set every element to 0?
A FOR Index ← 1 TO 11
B FOR Index ← 1 TO 10
3. What does this code do? FOR Index ← 1 TO 5 Scores[Index] ← 0 NEXT Index
A Sets Scores[1] to Scores[5] to 0, one element per pass
B Sets only Scores[5] to 0 after looping five times
4. Marks is declared as ARRAY[0:9] OF INTEGER. Which loop sets all the elements to 0?
A FOR Index ← 1 TO 10
B FOR Index ← 0 TO 9
5. Which statement about setting every element with a FOR loop is correct?
A The loop variable is used as the array index
B The loop variable is used as the value stored

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 StudentNames[Index] ← "" NEXT Index AFOR Index ← 1 TO 30
2Scores[Index] ← 0 BThe loop limit 31 is larger than the upper bound 30 of the array, so the last pass writes to an index that does not exist and causes an index out of range error. It should be TO 30.
3The loop limits must match the array bounds, so an array declared as ARRAY[1:30] is filled by a loop from 1 to 30 CFOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
4A FOR loop can set every element of a Scores array to 0 by using the loop variable as the index DSets all 30 elements of StudentNames to an empty string, one element on each pass of the loop
5FOR Index ← 1 TO 31 Scores[Index] ← 0 NEXT Index EAssigns 0 to the element at the position held in Index; inside a FOR loop this resets one element per pass
6FOR Index ← 1 TO 30 Scores[1] ← 0 NEXT Index FThe fixed index 1 means only the first element is set, thirty times. It should use the loop variable: Scores[Index] ← 0.

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 __ 30 Scores[Index] ← 0 NEXT Index
2.
FOR Index ← 1 TO 30 StudentNames[_____] ← "" NEXT Index
3.
// Scores : ARRAY[0:9] OF INTEGER FOR Index ← 0 TO _ Scores[Index] ← 0
4.
FOR Index ← 1 TO 30 Scores[Index] ← 0 ____ Index
5.
FOR Index ← 1 TO 30 Scores[Index] _ 0 NEXT Index

Spot the Error

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

Question 1
A // Scores is ARRAY[1:3]
B FOR Index ← 1 TO 3
C Scores[Index] ← 0
D NEXT Scores
Question 2
A FOR Index ← 30 TO 1
B Scores[Index] ← 0
C NEXT Index
Question 3
A FOR Index 1 TO 30
B StudentNames[Index] ← ""
C NEXT Index
Question 4
A // Scores is ARRAY[1:30]
B FOR Index ← 1 TO 31
C Scores[Index] ← 0
D NEXT Index
Question 5
A // Names is ARRAY[0:9]
B FOR Index ← 1 TO 9
C Names[Index] ← ""
D NEXT Index

Answer Key

Setting every element with a FOR loop
Section A: Quick Questions
Q1
A — FOR Index ← 1 TO 30 StudentNames[Index] ← "" NEXT Index
Q2
B — FOR Index ← 1 TO 10
Q3
A — Sets Scores[1] to Scores[5] to 0, one element per pass
Q4
B — FOR Index ← 0 TO 9
Q5
A — The loop variable is used as the array index
Section B: Match the Pairs
Q1
FOR Index ← 1 TO 30 StudentNames[Index] ← "" NEXT Index → Sets all 30 elements of StudentNames to an empty string, one element on each pass of the loop
Q2
Scores[Index] ← 0 → Assigns 0 to the element at the position held in Index; inside a FOR loop this resets one element per pass
Q3
The loop limits must match the array bounds, so an array declared as ARRAY[1:30] is filled by a loop from 1 to 30 → FOR Index ← 1 TO 30
Q4
A FOR loop can set every element of a Scores array to 0 by using the loop variable as the index → FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Q5
FOR Index ← 1 TO 31 Scores[Index] ← 0 NEXT Index → The loop limit 31 is larger than the upper bound 30 of the array, so the last pass writes to an index that does not exist and causes an index out of range error. It should be TO 30.
Q6
FOR Index ← 1 TO 30 Scores[1] ← 0 NEXT Index → The fixed index 1 means only the first element is set, thirty times. It should use the loop variable: Scores[Index] ← 0.
Section C: Definitions
Q1
FOR Index ← 1 TO 30 — A loop that uses its counter as the array index, so each pass assigns a value to one element, e.g. Scores[Index] ← 0. The loop limits must match the array bounds.
Section D: Fill in the Blanks
Q1
FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Q2
FOR Index ← 1 TO 30 StudentNames[Index] ← "" NEXT Index
Q3
// Scores : ARRAY[0:9] OF INTEGER FOR Index ← 0 TO 9 Scores[Index] ← 0
Q4
FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Q5
FOR Index ← 1 TO 30 Scores[Index] ← 0 NEXT Index
Section E: Spot the Error
Q1
D — NEXT must name the loop variable that FOR created. It should be NEXT Index, not the array name.
Q2
A — A FOR loop counts upwards unless a STEP is given, so 30 TO 1 never runs and no elements are set. It should be FOR Index ← 1 TO 30.
Q3
A — The FOR line needs the assignment arrow to give the loop variable its starting value. It should be FOR Index ← 1 TO 30.
Q4
B — The upper bound of Scores is 30, so the loop must stop at 30. Going to 31 tries to write to Scores[31], which is out of range.
Q5
B — The lower bound of Names is 0, so the loop must start at 0. Starting at 1 leaves Names[0] unset. It should be FOR Index ← 0 TO 9.