Off-by-one error
An off-by-one error is a programming or computational error in which a count, index, boundary, or result is exactly one greater or less than intended.
100 Java Mistakes and How to Avoid Them
An off-by-one error is a boundary error in which an iteration or indexing operation starts or stops one position or value too early or too late.
- How it is used
- Diagnoses boundary failures in Java indexing, range validation, and forward or reverse iteration.
- How deep it goes
- Explains failure modes, corrected loop forms, overflow-safe range checks, and analyzer limits without developing complete programs.
- Examples
- Uses empty strings, arrays, lists, contiguous ranges, descending for loops, and Java range-checking methods.
- What you can do after
- Diagnose and prevent boundary bugs with explicit checks, corrected loop bounds, Java validation methods, and targeted analyzers.
Read how 100 Java Mistakes and How to Avoid Them explains off-by-one error →
Software Design for Python Programmers
An off-by-one error is a computational error in which a value or operation count is exactly one greater or one less than intended.
- How it is used
- Prevents surprising runtime logic errors by making loop counts and index conventions explicit.
- How deep it goes
- Explains loop bounds and zero-based indexing through worked examples, then stops at two simple corrective designs.
- Examples
- Uses a six-iteration while loop and a Python Date class whose month lookup initially prints MAR for February.
- What you can do after
- Diagnose incorrect counts or values by checking loop boundaries and translating human-facing values before zero-based indexing.
Read how Software Design for Python Programmers explains off-by-one error →
100 C++ Mistakes and How to Avoid Them
An off-by-one error is a boundary error in which an iteration or index range includes one element too many or too few.
- How it is used
- Uses loop-bound mistakes to motivate safer ways to iterate over containers.
- How deep it goes
- Explains the mistaken bound and demonstrates a range-based correction, without pursuing broader boundary-error cases.
- Examples
- Uses a vector-summing loop and contrasts its index-based form with a range-based version.
- What you can do after
- Apply range-based for loops to iterate through containers without manually specifying index bounds.
Read how 100 C++ Mistakes and How to Avoid Them explains off-by-one error →
Go in Practice, Second Edition
An off-by-one error is a boundary mistake in which a loop or index begins, ends, or counts one position incorrectly.
- How it is used
- Uses table-driven tests to find incorrect loop starts and exclusive endpoints in generated output.
- How deep it goes
- Explains two boundary mistakes and demonstrates diagnosis through failing and passing Go test output.
- Examples
- Uses a Fizz Buzz loop, exact expected strings, table cases, and go test output.
- What you can do after
- Diagnose boundary mistakes with table-driven tests and correct loop starts, endpoints, and expected output.
Read how Go in Practice, Second Edition explains off-by-one error →
Also covered in
What to know first
- functionA function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- arrayAn array is an ordered collection of elements organized in one or more dimensions and accessible by position or index.
- exceptionAn exception is a runtime signal or value representing an abnormal condition that interrupts ordinary program flow and can be handled or propagated.
- loopA loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
Related concepts
- runtime logic errorproduces · A runtime logic error is a defect in a program’s computation or control flow that causes incorrect behavior during execution, even though the program can run.
- iteratoraddressed by · An iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.
- range-based for loopaddressed by · A range-based for loop is an iteration statement that traverses the elements of a range or container without explicit index or iterator management.
- indexingused by · Indexing is the process of assigning or using positions, keys, or lookup structures to identify and access elements or records.
- loopuses · A loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
Questions about off-by-one error
What is off-by-one error?
An off-by-one error is a programming or computational error in which a count, index, boundary, or result is exactly one greater or less than intended.
Which Manning books explain off-by-one error?
100 Java Mistakes and How to Avoid Them, Software Design for Python Programmers, 100 C++ Mistakes and How to Avoid Them and Go in Practice, Second Edition.
What should I know before learning off-by-one error?
Manning books that teach off-by-one error usually introduce function, array, exception and loop first.
What is off-by-one error a kind of?
Off-by-one error is a kind of error. A software error is a fault or unexpected condition in a program or its execution that causes incorrect behavior or prevents intended operation.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



