How many times is a for loop executed?
Consider the following code snippet:
  
  for(int i=0;i&lt;n;i++)

  
  The question I have is: whether this loop is executed $n$ times or $(n+1)$ times?


This loop executes $n$ times, not $n+1$ times. The first iteration of the loop has $i = 0$; the second has $i = 1$, and so on, up to the last iteration of the loop $i = (n-1)$. Then let us see what happens after that:


We get to the bottom of the loop  when $i = (n-1)$.
At this point, we jump up to the top and execute i++. So now $i = n$.
Next, we check the loop condition. We see that i &lt; n is false, so the loop condition is false. That means we exit the loop.


So in total, we executed the loop for $i = 0$, $i = 1$, $i = 2$, up to $i = n-1$. That is in total $n$ times.

Your misconception was that the loop is terminated once $i$ gets to $n + 1$; but actually it is terminated when $i = n$, because $n$ is already not less than $n$.


  I know this would not make any difference to the complexity since $O(n+1)=O(n)$, linear time.


That is correct!