The answer
Always ensure your loop condition uses strictly less than (<) the array's length, rather than less than or equal to (<=).
Always ensure your loop condition uses strictly less than (<) the array's length, rather than less than or equal to (<=).
It is a logical bug where a loop boundary is miscalculated, causing the loop to execute one time too many or one time too few.
It relates to memory addressing. The index represents the memory offset from the starting position of the array. The first element has an offset of 0.
Your code will run without crashing, but it will skip the final element, resulting in an incorrect output or a failed test case.
Browse all our FAQs or reach out to our support team
Your learning library
Find your next course. Build something great.