1

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++;
in-just
  • 194
  • 1
  • 3
  • 14
  • Why do you think post increment is far more common? – Ghazi Apr 19 '20 at 02:54
  • In my opinion `i++` reads more naturally than `++i` to indicate an increment (in the sense that it conveys the idea of the leftmost operand being the one assigned to as with normal assignment). Perhaps it is for this reason? – Apollo Apr 19 '20 at 03:07

2 Answers2

1

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.

Hello World
  • 305
  • 1
  • 9
0

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.

user13947194
  • 337
  • 5
  • 7