← ACARA Digital Technologies Years 7–8

Off-by-one errors in loops

A loop bug where it runs one pass too many or too few, caused by a wrong start value or mixing up < and <=. For example, i starting at 0 with i <= 5 runs 6 times, not 5.

Understanding Off-by-one errors in loops in ACARA Digital Technologies Years 7–8

What it does: Starting at 1 and including 5 gives the passes 1, 2, 3, 4, 5, which is 5 passes. i goes 1, 2, 3, 4 and then stops before 5, so there are only 4 passes. This is an off-by-one error. i < 5 stops as soon as i reaches 5, so the values are 0, 1, 2, 3 and 4. That is 5 passes. i <= 5 includes 5, so the loop runs for 0, 1, 2, 3, 4 and 5. That is one extra pass, which is an off-by-one error if you wanted 5. i < 10 stops before 10, so the number 10 is never printed. Use <= 10 when you start at 1 and want to include 10.

Common mistakes: i < n stops before i reaches 5, so the loop only prints 1 to 4. Use i <= n to include the last number. Starting at 0 with i <= 5 runs for i = 0, 1, 2, 3, 4 and 5, which is 6 passes. Use i < 5 to get exactly 5.

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 624–634) and word search (Modules 611–634).

Take this worksheet further — free with Cloodit

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

Learning
Duo
How many times does this loop run? for (let i = 0; i < 5; i++)
5 times (i is 0, 1, 2, 3, 4)6 times (i is 0, 1, 2, 3, 4, 5)
Matches
for (let i = 0; i <= 3; i++) { console.log("Hi"); } // want 3 greetings
↓
Starting at 0 with <= 3 runs 4 times, not 3. Change <= to < to get 3 passes.
Applying
Meanings
Term
off-by-one error
Meaning
A loop bug where it runs one pass too many or too few, caused by a wrong start value or mixing up < and <=. For example, i starting at 0 with i <= 5 runs 6 times, not 5.
Practice
for (let i = 0; i < 5; i++) {
Testing
Gaps
for (let i = 0; i 5; i++) {
Check Answer
Spots
2let n = 5;
3for (let i = 1; i < n; i++) {

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

The 'Fox' badge — Cunning Debugger, 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. How many times does this loop run? for (let i = 0; i < 5; i++)
A. 5 times (i is 0, 1, 2, 3, 4) ✓
B. 6 times (i is 0, 1, 2, 3, 4, 5)
i < 5 stops as soon as i reaches 5, so the values are 0, 1, 2, 3 and 4. That is 5 passes.
2. How many times does this loop run? for (let i = 0; i <= 5; i++)
A. 5 times (i is 0 to 4)
B. 6 times (i is 0 to 5) ✓
i <= 5 includes 5, so the loop runs for 0, 1, 2, 3, 4 and 5. That is one extra pass, which is an off-by-one error if you wanted 5.
3. You want a loop to run exactly 3 times. Which loop header does that?
A. for (let i = 1; i < 3; i++)
B. for (let i = 1; i <= 3; i++) ✓
Starting at 1 with <= 3 gives 1, 2, 3, which is 3 passes. Starting at 1 with < 3 gives only 1 and 2, so it runs one time too few.
4. Which loop header is a correct way to print the numbers 1 to 10?
A. for (let i = 1; i <= 10; i++) ✓
B. for (let i = 1; i < 10; i++)
i < 10 stops before 10, so the number 10 is never printed. Use <= 10 when you start at 1 and want to include 10.
5. This loop should run 4 times but runs 5 times. What is the likely cause? for (let i = 0; i <= 4; i++)
A. <= should be < so it stops at 3 ✓
B. i++ should be i-- to count correctly
Starting at 0 and using <= 4 gives 0, 1, 2, 3, 4, which is 5 passes. Changing to i < 4 gives exactly 4 passes.

Match the Pairs

for (let i = 0; i < 5; i++) → Runs exactly 5 times, with i going 0, 1, 2, 3, 4
for (let i = 0; i <= 5; i++) → Runs 6 times, with i going 0 to 5, which is one pass too many if you wanted 5
To repeat 5 times when counting from 1, use <= 5 so the last pass is when i is 5 → for (let i = 1; i <= 5; i++)
Starting at 1 but using < 5 stops early and runs only 4 times, one pass too few → for (let i = 1; i < 5; i++)
for (let i = 0; i <= 3; i++) { console.log("Hi"); } // want 3 greetings → Starting at 0 with <= 3 runs 4 times, not 3. Change <= to < to get 3 passes.
for (let i = 1; i < 10; i++) { total += i; } // want to add 1 to 10 → The loop stops when i is 9, so 10 is never added. Change < to <= to include 10.

Key Term

off-by-one error
A loop bug where it runs one pass too many or too few, caused by a wrong start value or mixing up < and <=. For example, i starting at 0 with i <= 5 runs 6 times, not 5.

Fill in the Blanks

1. for (let i = 0; i < 5; i++) { console.log(i); }
Hint: Type the comparison operator that stops the loop after exactly 5 passes when i starts at 0.
2. for (let i = 1; i <= 5; i++) { console.log(i); }
Hint: Type the operator that lets the loop still run when i equals 5, so it counts 1 to 5.
3. // Run 3 times: 1, 2, 3 for (let i = 1; i <= 3; i++) { }
Hint: Type the start value so that, with <=, the loop runs exactly 3 passes.
4. let passes = 0; for (let i = 0; i < 4; i++) { passes++; } console.log(passes);
Hint: Type the variable name that counts each pass through the loop so you can check the total.
5. // Run 7 times, starting at 0 for (let i = 0; i < 7; i++) { }
Hint: Type the number the loop limit needs to be so that i < limit gives 7 passes from 0.

Spot the Error

Question 1
A // Print 1 to 5
B let n = 5;
C for (let i = 1; i < n; i++) {
D console.log(i);
E }
Line C — i < n stops before i reaches 5, so the loop only prints 1 to 4. Use i <= n to include the last number.
Question 2
A let passes = 0;
B for (let i = 0; i <= 5; i++) {
C passes++;
D }
E console.log(passes); // 5
Line B — Starting at 0 with i <= 5 runs for i = 0, 1, 2, 3, 4 and 5, which is 6 passes. Use i < 5 to get exactly 5.
Question 3
A for (let i = 1; i < 3; i++) {
B console.log("Lap " + i);
C }
Line A — The laps should be numbered 1 to 3. With i < 3 the loop stops after Lap 2, so it runs one time too few. It should be i <= 3.
Question 4
A let laps = 0;
B // Run exactly 3 times
C for (let i = 0; i <= 3; i++) {
D laps++;
E }
Line C — Starting at 0 with i <= 3 gives the passes 0, 1, 2 and 3, which is 4 passes. Start at 1 (or use i < 3) to get exactly 3.
Question 5
A let n = 1;
B // Print 1, 2, 3
C while (n < 3) {
D console.log(n);
E n++;
F }
Line C — n < 3 becomes false when n is 3, so 3 is never printed and the loop runs one time too few. It should be n <= 3.

Student with a class code? Join your class →