Looping with a lower bound of zero

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

Looping with a lower bound of zero

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An array is declared as ARRAY[0:29] OF STRING. Which loop visits every element exactly once?
A FOR Index ← 1 TO 30
B FOR Index ← 0 TO 29
2. DECLARE Scores : ARRAY[0:9] OF INTEGER. Which loop header matches this declaration?
A FOR Index ← 0 TO 9
B FOR Index ← 1 TO 10
3. A student uses FOR Index ← 1 TO 30 on ARRAY[0:29]. What goes wrong?
A It skips element 0 and tries to use element 30, which is outside the array
B It works correctly because there are 30 loop repeats
4. For ARRAY[0:29], which loop starts at the lower bound?
A FOR Index ← 0 TO 29
B FOR Index ← 29 TO 0
5. Names is declared as ARRAY[0:4] OF STRING. Which loop goes outside the array?
A FOR Index ← 0 TO 4
B FOR Index ← 0 TO 5

Match the Pairs

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

Left columnRight column
1FOR Index ← 0 TO 29 AThe loop ends at 30, which is past the upper bound 29, so it goes out of range. It should end at 29.
2FOR Index ← 1 TO 30 BFOR Index ← 0 TO 9
3To loop through ARRAY[0:9], start at the lower bound and finish at the upper bound CThe loop starts at 1, so element 0 is never visited. It should start at the lower bound 0.
4The loop limits must match the declaration of the array ARRAY[0:49] DVisits every element of ARRAY[0:29] from the lower bound to the upper bound
5FOR Index ← 1 TO 29 (for ARRAY[0:29]) EMisses element 0 and goes past the end of ARRAY[0:29]
6FOR Index ← 0 TO 30 (for ARRAY[0:29]) FFOR Index ← 0 TO 49

Definitions

FOR Index ← 0 TO 29

Fill in the Blanks

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

1.
DECLARE Scores : ARRAY[0:29] OF INTEGER FOR Index ← _ TO 29
2.
DECLARE Names : ARRAY[0:29] OF STRING FOR Index ← 0 TO __
3.
DECLARE Marks : ARRAY[0:9] OF INTEGER FOR Index ← 0 TO _ OUTPUT Marks[Index]
4.
DECLARE Items : ARRAY[_:19] OF STRING FOR Index ← 0 TO 19
5.
FOR Index ← 0 __ 29 OUTPUT Scores[Index] NEXT Index

Spot the Error

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

Question 1
A // Mark is ARRAY[0:9]
B FOR I ← 1 TO 9
C Mark[I] ← 0
D NEXT I
Question 2
A // Mark is ARRAY[0:29]
B FOR Index ← 1 TO 29
C OUTPUT Mark[Index]
D NEXT Index
Question 3
A // Temp is ARRAY[0:29]
B Total ← 0
C FOR Index ← 0 TO 30
D Total ← Total + Temp[Index]
E NEXT Index
Question 4
A // Score is ARRAY[0:29]
B FOR Index ← 0 TO 28
C Score[Index] ← 0
D NEXT Index
Question 5
A DECLARE T : ARRAY[0:8] OF REAL
B FOR I ← 0 TO 9
C T[I] ← 0.5
D NEXT I

Answer Key

Looping with a lower bound of zero
Section A: Quick Questions
Q1
B — FOR Index ← 0 TO 29
Q2
A — FOR Index ← 0 TO 9
Q3
A — It skips element 0 and tries to use element 30, which is outside the array
Q4
A — FOR Index ← 0 TO 29
Q5
B — FOR Index ← 0 TO 5
Section B: Match the Pairs
Q1
FOR Index ← 0 TO 29 → Visits every element of ARRAY[0:29] from the lower bound to the upper bound
Q2
FOR Index ← 1 TO 30 → Misses element 0 and goes past the end of ARRAY[0:29]
Q3
To loop through ARRAY[0:9], start at the lower bound and finish at the upper bound → FOR Index ← 0 TO 9
Q4
The loop limits must match the declaration of the array ARRAY[0:49] → FOR Index ← 0 TO 49
Q5
FOR Index ← 1 TO 29 (for ARRAY[0:29]) → The loop starts at 1, so element 0 is never visited. It should start at the lower bound 0.
Q6
FOR Index ← 0 TO 30 (for ARRAY[0:29]) → The loop ends at 30, which is past the upper bound 29, so it goes out of range. It should end at 29.
Section C: Definitions
Q1
FOR Index ← 0 TO 29 — For ARRAY[0:29], the loop must start at the lower bound 0 and end at the upper bound 29. It then visits all 30 elements and never goes outside the array.
Section D: Fill in the Blanks
Q1
DECLARE Scores : ARRAY[0:29] OF INTEGER FOR Index ← 0 TO 29
Q2
DECLARE Names : ARRAY[0:29] OF STRING FOR Index ← 0 TO 29
Q3
DECLARE Marks : ARRAY[0:9] OF INTEGER FOR Index ← 0 TO 9 OUTPUT Marks[Index]
Q4
DECLARE Items : ARRAY[0:19] OF STRING FOR Index ← 0 TO 19
Q5
FOR Index ← 0 TO 29 OUTPUT Scores[Index] NEXT Index
Section E: Spot the Error
Q1
B — Mark has a lower bound of 0, so the loop must start at 0. Starting at 1 skips Mark[0]. The correct line is FOR I ← 0 TO 9.
Q2
B — The array runs from index 0 to 29. A loop starting at 1 misses element 0. The correct line is FOR Index ← 0 TO 29.
Q3
C — The upper bound is 29. Going up to 30 reads Temp[30], which is outside the array. The correct line is FOR Index ← 0 TO 29.
Q4
B — The loop stops one element early, so Score[29] is never set. It must run to the upper bound: FOR Index ← 0 TO 29.
Q5
A — The loop runs from 0 to 9, so the array needs an upper bound of 9. With [0:8], T[9] would be out of range. The declaration should be ARRAY[0:9].