I have a little confusion in my mind. I know very well the difference between preincrement and postincrement, postdecrement and predecrement.
But i wonder how do these operators work during loop.
I ,myself, use postincrement or decrement in loop as follows:
But in my book I read mostly preincrement as:
So ,in short, my point is ...What is the difference between these two loops?
I think this is an old micro-optimisation thing that is irrelevant with modern compilers. The preincrement just adds 1 but the postincrement theoretically requires you to store the initial value before incrementing. Nowdays that would be optimised out anyway.
I think this is an old micro-optimisation thing that is irrelevant with modern compilers.
It was irrelevant even in prehistoric times too, unless you were working with one of the dumbest compilers ever. Even Dennis Ritchie's first C compiler performed this optimization.
However, note that the type of i matters. For int (or any built-in type, generally), the two are functionally identical. For class types that overload the ++ operator, the prefix version will likely be slightly more efficient.