Code on a screen, representing an off-by-one error.

The classic bug of counting one too many or one too few.

What an off-by-one error is

An off-by-one error is a very common kind of programming mistake in which a calculation or loop is wrong by exactly one - doing something one time too many or one time too few. It typically shows up at the edges of a range, such as when a program counts through a list and either stops one item early or tries to go one item past the end. Despite being small, it is a frequent and sometimes tricky source of bugs.

Why it happens

Off-by-one errors often arise from confusion about where a range begins and ends, or whether a boundary should be included or not. For example, deciding whether a loop should run up to a number or up to and including it is exactly the kind of choice that, if gotten slightly wrong, produces an off-by-one error. These boundary decisions are easy to misjudge, which is why this type of mistake appears so often, even among experienced programmers.

A common example

A classic setting for off-by-one errors is working through the items of a list. If a program starts counting from the wrong place, or uses the wrong stopping point, it may skip the first or last item, or attempt to access an item that does not exist. The error is just a single step in the wrong direction, but the effect can range from a subtly incomplete result to a program crash, depending on what the off-by-one slip causes.

Why off-by-one errors matter

Because they are small, off-by-one errors can be easy to overlook, yet their consequences can be significant. Missing one item might corrupt a result quietly, while stepping one past the end of a range might cause a crash or, in some cases, a security weakness. The subtlety is exactly what makes them dangerous: everything looks almost right, and the single-step mistake can hide until it causes a visible problem.

Avoiding off-by-one errors

Programmers reduce off-by-one errors by thinking carefully about boundaries - being clear about where a range starts and stops and whether endpoints are included. Testing with small examples, especially checking the very first and very last cases, helps catch these mistakes. Many modern programming styles and tools also make it easier to work through collections without manual counting, which removes a common opportunity for off-by-one slips to creep in.

Why it matters

The off-by-one error is one of the most famous bugs in programming precisely because it is so easy to make and so easy to miss. Understanding what an off-by-one error is - being wrong by a single step, usually at a boundary - helps programmers watch for it and test the edges of their ranges. For anyone who writes code, recognizing this classic pitfall is a genuinely useful habit.

See also: What is a bug? Software bugs and debugging explained, What is a stack overflow error? Explained.

Sources

Published date reflects the original event date (2023-05-02). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.

Skillo Staff

Skillo Staff

All stories by: Skillo Staff

Leave a Reply