Seems like in most cases it would make more sense to use the pre yet somehow post is far more common.
while (counter < 10)
counter++;
Seems like in most cases it would make more sense to use the pre yet somehow post is far more common.
while (counter < 10)
counter++;
After reading everything I can find on the history of ++
and --
, it seems that there is no technical reason for this convention, if it is even a convention (more on that later). Hence, I assume it must be an issue of a personal preference evolved. I'd posit that it has to do with readability. counter++
could be seen as more intuitive than ++counter
because it is an assignment operator. Essentially, it's shorthand for counter = counter + 1
. Here you have the increment on the right being assigned to the left-hand expression. counter++
naturally better fulfills this logic.
However, it is not necessarily a convention. Some would argue that the prefix operator is always better (admittedly for mostly historical reasons). Personally, I prefer the prefix because it makes more sense to me in the context of other unary operators like &
, *
, etc. as well as the possible speed optimization.
Maybe it is there rebelious nature. "Who cares about speed?". ++i and i++ are just as easy to read. I didnt even concieve that persons would have a warm time understanding ++i. But where I am standing, there is no difficulty reading any of them. So I choose the one that has less overhead. It is not a big boost, but it is also not harder to type.