If we are talking about the effect of this on the overall result of the operation in the for loop, the answer is, it doesn’t matter. (I am assuming that i an type int variable.)
That said, this question arises when you examine program execution minutely. For instance, in the case of ++i, it’s a simple case of increment and use, but in the case of i++, it is use and then increment, in which case the old value of i has to be stored somewhere. Again, that said, it doesn’t really matter, since practically all compilers are smartly optimized to make this a non-issue.
But then languages like C++ allow the programmer to overload the ++ operator, both, in its pre- and post-increment forms. So it eventually boils down to how the programmer handles the pre- and post-increment operations in their own code. And this may sometimes entail saving the entire variable in a local location, and then doing the processing. So ++i may be more or equally efficient than i++, and is therefore often the operator of choice in for loops in C++. To know more about this, I would recommend you have a look at More Effective C++: 35 New Ways to Improve Your Programs and Designs by Scott Meyers (Amazon.com: More Effective C++: 35 New Ways to Improve Your Programs and Designs (0785342633719): Scott Meyers: Books); Specifically, the item on distinguishing between postfix and prefix increment operations.
Bottomline: This is a very subtle issue in a language like C++, which is the language of choice when you need to code to extract every single bit of speed and performance from the code. In other languages like, say Java or C#, or Python or Ruby, this doesn’t really matter, since the overall performance enhancement that you stand to gain by adapting this will be so miniscule as to be negligible.
Original post: https://www.quora.com/Why-doesnt-it-matter-if-we-use-I++-or-++I-for-a-for-loop/answer/Najeeb-Shaikh
Image courtesy: By Petar Milošević - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=42663476