Error: wrong loop bounds

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

Error: wrong loop bounds

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program should output the numbers 1 to 10. Which FOR line is correct?
A FOR Count ← 1 TO 10
B FOR Count ← 1 TO 9
2. What happens when this loop runs? FOR Num ← 1 TO 9 (the task is to process 10 numbers)
A It runs 9 times, one too few
B It runs 10 times, as needed
3. An array Scores[1:5] is 1-based. Which loop visits every element without going out of range?
A FOR Index ← 1 TO 5
B FOR Index ← 0 TO 5
4. Which statement about FOR Index ← 0 TO 4 is true?
A It runs 4 times, because the end value is 4
B It runs 5 times, so it is an off-by-one error if the task needs 4 items
5. Which tool is best for checking whether a loop's start and end values are correct?
A Adding more comments to the loop
B A trace table showing the loop variable on each pass

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 9 OUTPUT Count NEXT Count AThe loop starts at 0 but the array starts at 1, so it makes one extra pass. It should be FOR Index ← 1 TO 6.
2FOR Index ← 0 TO 4 OUTPUT Scores[Index] NEXT Index BFOR Count ← 1 TO 5 Total ← Total + Count NEXT Count
3To output all 10 values in a 1-based array of 10 items, the loop must start at 1 and end at 10. CThe end value is 9, so the loop stops one time too early and 10 is never added. It should be TO 10.
4A trace table of a loop shows the value of the counter on each pass and reveals whether the last value is missed. DFOR Index ← 1 TO 10 OUTPUT Marks[Index] NEXT Index
5FOR Num ← 1 TO 9 Total ← Total + Num NEXT Num // should add the numbers 1 to 10 EStarts at 0 on a 1-based array, so Scores[0] does not exist and the loop makes an extra pass.
6DECLARE Names : ARRAY[1:6] OF STRING FOR Index ← 0 TO 6 OUTPUT Names[Index] NEXT Index FRuns 9 times, not 10, so it misses the last value. This is an off-by-one error if 10 repeats were needed.

Definitions

off-by-one error

Fill in the Blanks

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

1.
FOR Count ← 1 TO __ OUTPUT Count NEXT Count
2.
FOR Index ← _ TO 5 OUTPUT Scores[Index] NEXT Index
3.
FOR Index ← 0 TO _ OUTPUT Names[Index] NEXT Index
4.
FOR Count ← 1 TO _ OUTPUT Count * 2 NEXT Count
5.
FOR Student ← 1 TO __ INPUT Marks[Student] NEXT Student

Spot the Error

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

Question 1
A DECLARE Total : INTEGER
B Total ← 0
C // add the numbers 1 to 10
D FOR Count ← 1 TO 9
E Total ← Total + Count
F NEXT Count
Question 2
A // Mark holds indexes 1 to 5
B Sum ← 0
C FOR Index ← 0 TO 5
D Sum ← Sum + Mark[Index]
E NEXT Index
Question 3
A // Score has indexes 1 to 10
B Sum ← 0
C // add every score
D FOR Index ← 1 TO 11
E Sum ← Sum + Score[Index]
F NEXT Index
Question 4
A // show the 7 times table
B // from 7 * 1 up to 7 * 12
C FOR Number ← 2 TO 12
D OUTPUT 7 * Number
Question 5
A // Rain has indexes 1 to 7
B FOR Day ← 1 TO 6
C OUTPUT Rain[Day]
D NEXT Day

Answer Key

Error: wrong loop bounds
Section A: Quick Questions
Q1
A — FOR Count ← 1 TO 10
Q2
A — It runs 9 times, one too few
Q3
A — FOR Index ← 1 TO 5
Q4
B — It runs 5 times, so it is an off-by-one error if the task needs 4 items
Q5
B — A trace table showing the loop variable on each pass
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 9 OUTPUT Count NEXT Count → Runs 9 times, not 10, so it misses the last value. This is an off-by-one error if 10 repeats were needed.
Q2
FOR Index ← 0 TO 4 OUTPUT Scores[Index] NEXT Index → Starts at 0 on a 1-based array, so Scores[0] does not exist and the loop makes an extra pass.
Q3
To output all 10 values in a 1-based array of 10 items, the loop must start at 1 and end at 10. → FOR Index ← 1 TO 10 OUTPUT Marks[Index] NEXT Index
Q4
A trace table of a loop shows the value of the counter on each pass and reveals whether the last value is missed. → FOR Count ← 1 TO 5 Total ← Total + Count NEXT Count
Q5
FOR Num ← 1 TO 9 Total ← Total + Num NEXT Num // should add the numbers 1 to 10 → The end value is 9, so the loop stops one time too early and 10 is never added. It should be TO 10.
Q6
DECLARE Names : ARRAY[1:6] OF STRING FOR Index ← 0 TO 6 OUTPUT Names[Index] NEXT Index → The loop starts at 0 but the array starts at 1, so it makes one extra pass. It should be FOR Index ← 1 TO 6.
Section C: Definitions
Q1
off-by-one error — A FOR loop with the wrong start or end value runs one time too many or too few, e.g. TO 9 instead of TO 10, or starting at 0 for a 1-based array. Check the bounds with a trace table.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Q2
FOR Index ← 1 TO 5 OUTPUT Scores[Index] NEXT Index
Q3
FOR Index ← 0 TO 4 OUTPUT Names[Index] NEXT Index
Q4
FOR Count ← 1 TO 3 OUTPUT Count * 2 NEXT Count
Q5
FOR Student ← 1 TO 20 INPUT Marks[Student] NEXT Student
Section E: Spot the Error
Q1
D — This is an off-by-one error. The comment says to add 1 to 10, but TO 9 stops one time too soon, so 10 is never added. It should be FOR Count ← 1 TO 10.
Q2
C — The array is 1-based, so index 0 does not exist. Starting at 0 also makes the loop run 6 times instead of 5. It should be FOR Index ← 1 TO 5.
Q3
D — Score only has indexes 1 to 10. TO 11 makes the loop run one time too many and reads Score[11], which is out of range. It should be FOR Index ← 1 TO 10.
Q4
C — The table should start at 7 * 1, but starting at 2 skips the first line and the loop runs 11 times instead of 12. It should be FOR Number ← 1 TO 12.
Q5
B — There are 7 days of rain data, but TO 6 makes the loop run one time too few, so Rain[7] is never output. It should be FOR Day ← 1 TO 7.