Error: loop repeats one time too many or too few

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

Error: loop repeats one time too many or too few

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A loop must run 5 times. The counter starts at 1 and goes up by 1 each time. Which stop test is correct?
A REPEAT UNTIL count > 5
B REPEAT UNTIL count = 5
2. count ← 0, then REPEAT UNTIL count = 5 { OUTPUT 'Go' then count ← count + 1 }. How many times does the loop body run?
A 4 times
B 5 times
3. count ← 1, then REPEAT UNTIL count > 3 { OUTPUT count then count ← count + 1 }. How many body rows are in the trace table?
A 3 rows (output 1, 2, 3)
B 4 rows (output 1, 2, 3, 4)
4. An algorithm should print the numbers 1 to 4, but the output is 1, 2, 3, 4, 5. What is the most likely bug?
A A syntax error: OUTPUT is spelled wrong
B An off-by-one error: the loop runs one time too many
5. count ← 0, then WHILE count <= 5 { count ← count + 1 }. The goal is 5 repeats. What happens?
A It runs 6 times, because the counter starts at 0 and includes 5
B It runs exactly 5 times

Match the Pairs

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

Left columnRight column
1count ← 0 REPEAT UNTIL count > 5 count ← count + 1 Acount ← 0 REPEAT UNTIL count = 5 count ← count + 1
2count ← 1 WHILE count < 5 count ← count + 1 Bcount ← 1 REPEAT UNTIL count = 5 count ← count + 1
3Fix for a loop that runs one time too many: start the counter at 0 and stop when count = 5, giving exactly 5 passes. CRuns 6 times (count is 0, 1, 2, 3, 4, 5), which is one time too many if the goal was 5 repeats.
4The start value of the counter matters: starting at 1 instead of 0 with the stop test count = 5 gives only 4 passes. DThe stop test count > 3 lets the loop run when count is 3, so Hello is said 4 times instead of 3. Use count = 3 as the stop test.
5Goal: say Hello 3 times count ← 0 REPEAT UNTIL count > 3 OUTPUT "Hello" count ← count + 1 EThe test count < 4 stops after count 3, so Hi is said only 4 times instead of 5. Use count < 5 so the loop keeps going.
6Goal: say Hi 5 times count ← 0 WHILE count < 4 OUTPUT "Hi" count ← count + 1 FRuns only 4 times (count is 1, 2, 3, 4), which is one time too few if the goal was 5 repeats.

Definitions

off-by-one error

Fill in the Blanks

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

1.
count ← _ REPEAT UNTIL count > 5 count ← count + 1
2.
count ← 0 REPEAT UNTIL count _ 5 count ← count + 1
3.
// Trace table must show 5 rows REPEAT _ TIMES OUTPUT "Go"
4.
count ← 0 WHILE count _ 3 count ← count + 1
5.
count ← 0 REPEAT UNTIL count = 4 count ← count + _

Spot the Error

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

Question 1
A SET count ← 1
B SET total ← 0
C REPEAT UNTIL count = 5
D SET total ← total + count
E SET count ← count + 1
F END REPEAT
Question 2
A SET total ← 0
B OUTPUT "Adding laps 1 to 5"
C SET lap ← 0
D WHILE lap <= 5
E SET total ← total + lap
F SET lap ← lap + 1
Question 3
A OUTPUT "Eight claps"
B SET claps ← 0
C SET target ← 8
D REPEAT UNTIL claps > target
E OUTPUT "Clap"
F SET claps ← claps + 1
Question 4
A REPEAT 5 TIMES
B MOVE FORWARD 10
C TURN RIGHT 90
D END REPEAT
E OUTPUT "Square done"
Question 5
A OUTPUT "Days 1 to 7"
B SET day ← 2
C WHILE day <= 7
D OUTPUT day
E SET day ← day + 1
F END WHILE

Answer Key

Error: loop repeats one time too many or too few
Section A: Quick Questions
Q1
A — REPEAT UNTIL count > 5
Q2
B — 5 times
Q3
A — 3 rows (output 1, 2, 3)
Q4
B — An off-by-one error: the loop runs one time too many
Q5
A — It runs 6 times, because the counter starts at 0 and includes 5
Section B: Match the Pairs
Q1
count ← 0 REPEAT UNTIL count > 5 count ← count + 1 → Runs 6 times (count is 0, 1, 2, 3, 4, 5), which is one time too many if the goal was 5 repeats.
Q2
count ← 1 WHILE count < 5 count ← count + 1 → Runs only 4 times (count is 1, 2, 3, 4), which is one time too few if the goal was 5 repeats.
Q3
Fix for a loop that runs one time too many: start the counter at 0 and stop when count = 5, giving exactly 5 passes. → count ← 0 REPEAT UNTIL count = 5 count ← count + 1
Q4
The start value of the counter matters: starting at 1 instead of 0 with the stop test count = 5 gives only 4 passes. → count ← 1 REPEAT UNTIL count = 5 count ← count + 1
Q5
Goal: say Hello 3 times count ← 0 REPEAT UNTIL count > 3 OUTPUT "Hello" count ← count + 1 → The stop test count > 3 lets the loop run when count is 3, so Hello is said 4 times instead of 3. Use count = 3 as the stop test.
Q6
Goal: say Hi 5 times count ← 0 WHILE count < 4 OUTPUT "Hi" count ← count + 1 → The test count < 4 stops after count 3, so Hi is said only 4 times instead of 5. Use count < 5 so the loop keeps going.
Section C: Definitions
Q1
off-by-one error — A bug where a repeat count or stop test is wrong by one, such as count > 5 instead of count = 5, so the loop body runs one time too many or one time too few.
Section D: Fill in the Blanks
Q1
count ← 1 REPEAT UNTIL count > 5 count ← count + 1
Q2
count ← 0 REPEAT UNTIL count = 5 count ← count + 1
Q3
// Trace table must show 5 rows REPEAT 5 TIMES OUTPUT "Go"
Q4
count ← 0 WHILE count < 3 count ← count + 1
Q5
count ← 0 REPEAT UNTIL count = 4 count ← count + 1
Section E: Spot the Error
Q1
C — Count starts at 1, so the loop must run until count has gone past 5. Stopping when count = 5 ends the loop one time too early, so 5 is never added. It should be REPEAT UNTIL count > 5.
Q2
C — The laps should be 1 to 5, so lap must start at 1. Starting at 0 makes the loop run 6 times, one time too many, and the trace table gets an extra row. It should be SET lap ← 1.
Q3
D — Claps starts at 0 and goes up by 1, so it equals 8 after exactly 8 claps. Using > waits until claps is 9, so there is one extra clap. It should be REPEAT UNTIL claps = target.
Q4
A — A square has 4 sides, so the loop body must run 4 times. REPEAT 5 TIMES draws one extra side, which is an off-by-one error. It should be REPEAT 4 TIMES.
Q5
B — The program should show days 1 to 7, so day must start at 1. Starting at 2 skips day 1 and the loop runs one time too few. It should be SET day ← 1.