An off-by-one error (OBOE) means your loop runs one time too many or one time too few. It's the most frequent bug in loop code, and it comes from confusing < with <= or starting at the wrong index.
Consider printing the numbers to :
// Bug: prints 1 to 4 (misses 5)
for (int i = 1; i < 5; i++) {
System.out.println(i);
}
// Fixed: prints 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
The fix is a single character: < versus <=. To catch these bugs, always test your loops at the boundaries. Ask: "Does this include the first value? Does it include the last value?" Run the loop mentally with the smallest and largest values to verify.