Off-by-one errors in loops

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

Off-by-one errors in loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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)
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)
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++)
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++)
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

Match the Pairs

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

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

Definitions

off-by-one error

Fill in the Blanks

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

1.
for (let i = 0; i _ 5; i++) { console.log(i); }
2.
for (let i = 1; i __ 5; i++) { console.log(i); }
3.
// Run 3 times: 1, 2, 3 for (let i = _; i <= 3; i++) { }
4.
let passes = 0; for (let i = 0; i < 4; i++) { ______++; } console.log(passes);
5.
// Run 7 times, starting at 0 for (let i = 0; i < _; i++) { }

Spot the Error

Shade in the letter of the line that contains an 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 }
Question 2
A let passes = 0;
B for (let i = 0; i <= 5; i++) {
C passes++;
D }
E console.log(passes); // 5
Question 3
A for (let i = 1; i < 3; i++) {
B console.log("Lap " + i);
C }
Question 4
A let laps = 0;
B // Run exactly 3 times
C for (let i = 0; i <= 3; i++) {
D laps++;
E }
Question 5
A let n = 1;
B // Print 1, 2, 3
C while (n < 3) {
D console.log(n);
E n++;
F }

Answer Key

Off-by-one errors in loops
Section A: Quick Questions
Q1
A — 5 times (i is 0, 1, 2, 3, 4)
Q2
B — 6 times (i is 0 to 5)
Q3
B — for (let i = 1; i <= 3; i++)
Q4
A — for (let i = 1; i <= 10; i++)
Q5
A — <= should be < so it stops at 3
Section B: Match the Pairs
Q1
for (let i = 0; i < 5; i++) → Runs exactly 5 times, with i going 0, 1, 2, 3, 4
Q2
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
Q3
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++)
Q4
Starting at 1 but using < 5 stops early and runs only 4 times, one pass too few → for (let i = 1; i < 5; i++)
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
for (let i = 0; i < 5; i++) { console.log(i); }
Q2
for (let i = 1; i <= 5; i++) { console.log(i); }
Q3
// Run 3 times: 1, 2, 3 for (let i = 1; i <= 3; i++) { }
Q4
let passes = 0; for (let i = 0; i < 4; i++) { passes++; } console.log(passes);
Q5
// Run 7 times, starting at 0 for (let i = 0; i < 7; i++) { }
Section E: Spot the Error
Q1
C — i < n stops before i reaches 5, so the loop only prints 1 to 4. Use i <= n to include the last number.
Q2
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.
Q3
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.
Q4
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.
Q5
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.