educative.io

Educative

Why the number of iterations in the for loop for an array of n elements is n + 1?

for (int i = 0; i < input.length; i++)

Could you please explain why the number of iterations for this for loop is n+1 instead of n? thanks

Because while i = input.length , the expression i < input.length still need to be execute to determine whether i is smaller than input length or not. So, i iterative from 0 to input.length(=n), which is (0 -> n) , total is n + 1 times