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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
off-by-one errorFill in the Blanks
Spot the Error
Student with a class code? Join your class →