Im learning C and saw the first loop listed below in the book im reading. Im curious whats the difference between the two as I am used to using the second one and cant figure out the difference even though they return different results.
for(i = 0; i < 10; ++i){}
for(i = 0; i <= 10; i++){}
The first one iterates to
9, the second iterates to10. That’s all.The pre-/post- increment operation makes no difference.
Un-optimized code generated for both versions:
So, even here, there is no performance penalty. The fact that
i++is slower than++iis just not true (at least in this context, where it doesn’t make a difference). It would be slower for, sayint y = i++, but in this case, the two would do different things, which is not the case here. The performance issue might have been valid on compilers from 20 years ago, but not anymore.