
To fix an off-by-one error, write down the first value the loop should use, the last value it should use, and whether the stop boundary is included. Then trace the actual values produced and test an empty input, a one-item input, and a normal input. The first missing or extra value usually reveals whether the start, stop, or step is wrong.
This article is for programmers who have a loop that runs once too many, once too few, or touches an invalid array index. The method works across languages even though the syntax differs: reason about the sequence of index values before staring harder at the code.
An off-by-one error happens when a boundary calculation differs from the intended boundary by exactly one. A loop may visit 0, 1, 2, and 3 when it should also visit 4. Or it may attempt index 5 in a five-element zero-indexed array whose valid indices end at 4.
The usual cause is not the loop keyword. It is a mismatch between three related ideas: a count, a last index, and a stopping value. Five elements is a count. In a zero-indexed array, 4 is the last valid index. For a half-open range, 5 is the exclusive stop. Treating those three numbers as interchangeable creates the bug.
A loop that stops early can silently produce the wrong total, omit a character, or leave the final record unprocessed. A loop that runs late can read beyond the collection. Some languages throw an out-of-bounds exception; other environments may return an unusable value or expose memory-safety risks. The visible symptom depends on the language, but the faulty boundary reasoning is the same.
These bugs also survive casual testing. A five-item example may look almost correct because four items were processed. A one-item or empty input is less forgiving: the same assumption can become an immediate missing result or invalid access. Boundary-focused tests turn that inconvenience into a debugging advantage.
Before editing the condition, make a tiny table with three entries. Start is the first value used. Stop is either the last included value or the first excluded value, so label which convention you mean. Step is the change after each iteration, such as plus 1. This separates the intended sequence from the syntax that generates it.
Half-open ranges are useful because the stop value can equal the number of elements. Python's official documentation, for example, shows that range(5) produces 0 through 4 and never includes the endpoint 5. The same reasoning appears in the common loop condition i < length.

Suppose a loop should sum five values stored at indices 0 through 4. Its index starts at 0, increases by 1, and continues while i < 4. Trace it instead of guessing: 0 passes, 1 passes, 2 passes, 3 passes, and 4 fails the condition. Only four values reach the body, so index 4 is missing.
If 4 is the last value that must be included, a half-open loop needs the exclusive stop 5. Changing the condition to i < 5 produces 0, 1, 2, 3, 4 and then stops when i becomes 5. An inclusive condition could express the same intention as i <= 4, but mixing inclusive and exclusive conventions while debugging makes mistakes easier. Pick one convention and label it.
For a loop over the entire array, replace the unexplained literal 5 with the collection length when the language and task allow it. Then a five-item array gives i < 5, a one-item array gives i < 1, and an empty array gives i < 0. The condition now describes the collection instead of depending on a number that can drift away from it.
Do not stop after the five-item trace passes. Test an empty array, a one-item array, and the original five-item array. A correct full-array loop should execute zero, one, and five times respectively. The University of California San Diego teaching notes recommend exactly this style of array testing because empty, single-element, and longer arrays expose different assumptions.
The empty case can reveal a second unsafe access outside the loop. Code sometimes fixes the loop and then separately reads the first item, last item, or values[length - 1]. When length is 0, length - 1 is -1, which is not a valid array index. The loop may now be correct while the surrounding function is still unsafe.
The one-item case checks a different boundary. The first index and last index are both 0, so logic that assumes they are different may duplicate or skip the only value. Run these tests after every boundary change; they test the whole function, not merely the line you edited.

It visits 0, 1, and 2. The value 3 is the exclusive stop, so it never reaches the loop body.
Starting at 0 and continuing while i < n visits 0 through n - 1. For n = 0, the body executes zero times.
It checks whether code around the loop still assumes a first or last element exists. A correct zero-iteration loop does not protect a separate access such as values[length - 1].
If boundary traces keep getting lost inside dense lecture notes, Snitchnotes can turn your own programming material into clearer study notes, flashcards, quizzes, and audio review.
University of California San Diego: Arrays, Variables, and For-Loops 2
Illinois Institute of Technology: Off-By-One Error
Notatki, quizy, podcasty, fiszki i czat — z jednego uploadu.
Stwórz pierwszą notatkę za darmo