What is Off-by-one error?

A mistake in which a loop count or an array range is off by exactly one. It arises from whether the final value is included, whether indices start at 0 or 1, and confusing the number of intervals with the number of points (the fence-post problem). Tracing with the boundary values makes it easier to find.

Japanese name: オフバイワンエラー

Off-by-one error appears in 2 practice questions across 1 topic, including Programming Basics (Pseudocode Tracing).

Field: Programming Basics (Pseudocode Tracing)

Off-by-one error in the explanations

Questions that mention Off-by-one error (2)

  1. The following program was meant to compute the sum of all elements of array a. Array indices start at 1. What is the value of s after it runs?
  2. The following for loop increases its control variable by 5. What is the value of c after it runs?

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Array Variable Argument Bitwise operation Control structure Integer division Modulo operation Object-oriented programming Pseudocode Return value

Back to the glossary