Search code examples
post-incrementpre-increment

difference between ++i and i++ in for loop


I understand well how postfix and prefix increments/decrements work. But my question is, in a for loop, which is more efficient or faster, and which is more commonly used and why?

Prefix?

for(i = 0; i < 3; ++i) {...}

Or postfix?

for(i = 0; i < 3; i++) {...}

Solution

  • In this particular case, none is actually more efficient than the other. I would expect ++i to be more commonly used, because that's what would be more efficient for other kinds of iterations, like iterator objects.