liveBook · Manning

Off-by-one error

An off-by-one error is a boundary or counting mistake in which an index, range, iteration, or computed value is exactly one too high or low.

How 4 Manning books explain it

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
Prevents classic loop-boundary mistakes by favoring range-based iteration over manual index mechanics.
How deep it goes
Defines the boundary error, shows its misleading continuation test, and explains the safer range-based alternative.
Examples
Uses indexed and range-based vector summation examples, including the problematic <= continuation test.
What you can do after
Apply it via range-based for loops to iterate over containers without manually writing boundary tests.

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 →

Related concepts

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.