← ACARA Digital Technologies Years 7–8

Error: loop repeats one time too many or too few

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.

Understanding Error: loop repeats one time too many or too few in ACARA Digital Technologies Years 7–8

What it does: The counter goes 0→1, 1→2, 2→3, 3→4, 4→5 and then stops. That is 5 passes, so the trace table has exactly 5 loop rows. With count = 5 the loop stops as soon as count reaches 5, so the body only runs 4 times (count = 1, 2, 3, 4). Using count > 5 lets it run for 1, 2, 3, 4 and 5. The algorithm runs, but the stop test is wrong by one, so the body repeats one extra time. That makes it a logic error.

Common mistakes: 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. 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 420–481) and word search (Modules 420–519).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A loop must run 5 times. The counter starts at 1 and goes up by 1 each time. Which stop test is correct?
REPEAT UNTIL count = 5REPEAT UNTIL count > 5
Matches
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.
Applying
Meanings
Term
off-by-one error
Meaning
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.
Practice
SET count ← 1
Testing
Gaps
count ←
Check Answer
Spots
2SET total ← 0
3REPEAT UNTIL count = 5

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Penguin' badge — List Waddler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
With count = 5 the loop stops as soon as count reaches 5, so the body only runs 4 times (count = 1, 2, 3, 4). Using count > 5 lets it run for 1, 2, 3, 4 and 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 ✓
count is 0, 1, 2, 3 and 4 when the body runs, so there are 5 rows in the trace table. The loop stops when count becomes 5.
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)
The loop stops when count becomes 4, and the body never runs for 4. So the table has 3 body rows.
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 ✓
The algorithm runs, but the stop test is wrong by one, so the body repeats one extra time. That makes it a logic error.
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
count takes the values 0, 1, 2, 3, 4 and 5 when the body runs. That is 6 rows, so the start value or the stop test needs to change.

Match the Pairs

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.
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.
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
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
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.
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.

Key Term

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.

Fill in the Blanks

1. count ← 1 REPEAT UNTIL count > 5 count ← count + 1
Hint: Type the start value of the counter so the stop test count > 5 lets the loop body run exactly five times.
2. count ← 0 REPEAT UNTIL count = 5 count ← count + 1
Hint: Type the comparison operator that stops the loop exactly when the counter reaches 5, not one pass later.
3. // Trace table must show 5 rows REPEAT 5 TIMES OUTPUT "Go"
Hint: Type the repeat count that matches the number of rows the trace table should have.
4. count ← 0 WHILE count < 3 count ← count + 1
Hint: Type the relational operator that keeps the loop going only while count is below 3, so it runs 3 times and not 4.
5. count ← 0 REPEAT UNTIL count = 4 count ← count + 1
Hint: Type the amount added to the counter each pass so every loop adds just one row to the trace table.

Spot the 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
Line 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.
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
Line 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.
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
Line 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.
Question 4
A REPEAT 5 TIMES
B MOVE FORWARD 10
C TURN RIGHT 90
D END REPEAT
E OUTPUT "Square done"
Line 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.
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
Line 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.

Student with a class code? Join your class →