🧩Off-by-One Error
Also known as Off by One, OBOE, Fencepost Error
A loop or count that goes one step too far or stops one step short.
Think of it like
It is like posts in a fence. Ten gaps need eleven posts, and people often say ten.
Example
Why it matters
It is one of the most common bugs in all of coding. It hides at the edges, so a quick test of the middle misses it.
Definition
Example code
python
names = ["Ana", "Ben", "Cy", "Di", "Ed"]
for i in range(5):
print(names[i]) # 0 to 4, right
print(names[5]) # IndexErrorCommon mistake
Key takeaways
- 1Edges matter. The first and last steps go wrong.
- 2Zero-based count. Five items end at index 4.
- 3Check < and <=. One sign moves the end by one.