Cognitive Scaffold

Preparing your thinking workspace

arrow_back_ios_new
MENTAL MODEL · M0261

Off-by-one Error

Off-by-one Error
StructureHigh supportLogic
Included
account_tree

Updated 2026-07-28

Loading revision record…

Summary

An off-by-one error (OBOE) is an error in counting where the result is one more or one less than it should be due to a mistake in judging boundary conditions. It typically refers to a programming error in which a loop iterates one time too many or one time too few, and is a type of logical error. For example, a programmer might use "less than" instead of "less than or equal to" in a loop comparison, or fail to account for a sequence starting at 0 rather than 1 (as is the case with array indices in many programming languages). This error also occurs in mathematics.

Definition

An off-by-one error (OBOE) is an error in counting where the result is one more or one less than it should be due to a mistake in judging boundary conditions. It typically refers to a programming error in which a loop iterates one time too many or one time too few, and is a type of logical error. For example, a programmer might use "less than" instead of "less than or equal to" in a loop comparison, or fail to account for a sequence starting at 0 rather than 1 (as is the case with array indices in many programming languages). This error also occurs in mathematics.

How to use

An off-by-one error (OBOE) is an error in counting where the result is one more or one less than it should be due to a mistake in judging boundary conditions. It typically refers to a programming error in which a loop iterates one time too many or one time too few, and is a type of logical error. For example, a programmer might use "less than" instead of "less than or equal to" in a loop comparison, or fail to account for a sequence starting at 0 rather than 1 (as is the case with array indices in many programming languages). This error also occurs in mathematics.

Mechanism

In counting, indexing, and boundary handling, a deviation in the starting/ending point (e.g., counting from 0 vs. 1) leads to a result that is off by one. The root cause is confusion between "interval endpoints" and "counting" (the number of posts equals the number of intervals plus one).

Practice

In progress 0/4

Practice this model in one real situation:

Check to track your progress (stored locally)
Learning progress0%

Cases

  • A fence with 10 intervals requires 11 posts; a loop boundary misses the last element; pagination with "page 1, items 0-9" has an index offset.

Common pitfalls

  • Only guarding against it in code—this error can occur in any range, period, or step problem; relying on visual inspection of boundaries is highly prone to missing it.
Sources

Source support: Explicit

References currently contain only Wikipedia links; primary sources still need review.

  • link
    zh.wikipedia.orghttps://zh.wikipedia.org/wiki/%E5%B7%AE%E4%B8%80%E9%94%99%E8%AF%AFZH · Explicit
    verified
account_treeGenealogyexpand_more

RELATED MODELS