11

Disclaimer: I'm a rather new programming, so this question might be silly.

In the past, whenever I've wanted to increase or decrease an integer, I would use integer++ or integer--. However, after reading more programming books, I've discovered the operators += and -= (which upon further research, I discovered are known as the additive and subtractive assignment operators).

Obviously the assignment operators are most robust as you can vary the amount that you want to increase or decrease an integer by. What I'm wondering is: are there are any benefits or disadvantages to using integer++ vs. integer += 1?

3 Answers 3

17

integer++ actually does a bit more than you might think.

'++' after an integer first returns the value of integer and then increments integer:

int i = 5;
int a = i++; 
//a is now 5
//i is now 6.
i++;
//i iw now 7

You can also do ++integer which first increments the integer and then returns the value.

int i = 5;
int a = ++i;
//i and a are now 6.

As to which operator is better? It comes down to personal preference. Sven points out in the comments that both functions will output nearly identical instructions.

(everything I said is also true for --)

6
  • There is no performance difference for plain integer types. It does matter for complex C++ objects with overloaded operators, because there temporary copies are required.
    – Sven
    May 7, 2011 at 9:09
  • Under the hood i++ is slower when the compiler doesn't optimize it away/replaces it for ++i (when applicable). a = i++ actually translates to something like: tmp = i+1; a = i; i = tmp;. Of course this optimization is easy and always done when possible, so you are partially right :).
    – Roy T.
    May 7, 2011 at 10:12
  • Interesting, I never knew ++i and i++ did different things, thanks for the answer!
    – Kevin Yap
    May 7, 2011 at 15:09
  • 2
    Not exactly. On the assembly level this is the same: a = i++ gets translated to something like load i to r1, store r1 to a, increment r1, store r1 to i and a = ++i gets translated to load i to r1, increment r1, store r1 to i, store r1 to a (with a register r1 and the memory locations i and a). The only difference is the order of the increments and stores.
    – Sven
    May 7, 2011 at 15:50
  • Great info, now I am assuming you're right, but in programming 101 class I was told that a=i++ was slightly slower because it involved creating an extra variable, but your example just makes a lot more sense.
    – Roy T.
    May 7, 2011 at 16:10
1

++someInteger and someInteger += 1 are exactly the same, the first is just a shorter way to write the second. If you use this in an expression there is a difference between someInteger++ and ++someInteger though, as Roy T. pointed out.

But you really shouldn’t be thinking about this, just use what feels more natural to you. This certainly doesn’t matter for performance.

0

Also, just add to this thread... you may also find doing ++integer as opposed to integer++ to be useful (convenient) in some situations.

Note that integer++ or ++integer doesnt make a difference when you use in a for-loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.