1

I have a program set up right below.

for (int i = 0; i <= 10; i += 1) {
    cout << "+= " << i << endl;
}
cout << endl;
for (int i = 0; i <= 10; i = i + 1) {
    cout << "+ " << i << endl;
}

I assume += 1 is a shortcut for i = i + 1 because I can't just do (i = 0; i <= 10; i + 1), that just takes i, which is zero, and adds a one which makes a sum of just one. It does not actually do anything to i during the loop.

I'm sorry if I just answered my own question, I probably did but I couldn't find an answer anywhere else, and I just want to make sure.

DanTheMan
  • 61
  • 2
  • 7
  • 1
    Besides being a shortcut, it also only evaluates each operand once. Which can have a difference in certain contexts. And I'm sorry, but what is your question exactly again? – StoryTeller - Unslander Monica Jan 16 '18 at 06:19
  • 1
    `++i` or `i++` to increment by one. – Blacktempel Jan 16 '18 at 06:20
  • 1
    You can' depend on it. `+=` can be overloaded and do something unexpected. That's not good practice of course. – doug Jan 16 '18 at 06:23
  • 2
    Instead of guessing you might want to learn the language from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Blastfurnace Jan 16 '18 at 06:23
  • 1
    It also helps to have a good online language reference: [assignment operators](http://en.cppreference.com/w/cpp/language/operator_assignment) – Blastfurnace Jan 16 '18 at 06:27
  • 1
    @doug -- you absolutely can depend on it. You can't overload operators for built-in types, so `int i = 0; i += 1;` will **always** produce 1. – Pete Becker Jan 16 '18 at 13:08
  • @PeteBecker: You're right. Been working too long with user defined types. – doug Jan 16 '18 at 15:10
  • Why can't you just look up the `+=` operator on a search engine? There are (countably) infinite possible programs, and SO can't afford all of them. – user202729 Jan 16 '18 at 15:13

2 Answers2

2

TL:DR Yes all your assumptions are basically correct.

  • i += 1 takes i and adds 1 to it

  • ++i does the same

  • i + 1 does the same but does not store the result in i again so it won't be useful.

Just as a tip: i *= 2 will double i

Lenard
  • 138
  • 1
  • 9
  • A quote from standard would make this a great answer. I tried to find one and thought it should be easy, but it is harder than I expected. – Yunnosch Jan 16 '18 at 06:31
  • 6
    `i += 1` is the same as `++i`, not `i++` – M.M Jan 16 '18 at 06:34
  • @M.M An answer elaborating on the difference, with an explanation how the difference affects OPs code behaviour or how the difference becomes visible in other examples (maybe with details what affects the visibility of the difference) would be interesting. – Yunnosch Jan 16 '18 at 06:46
  • @Yunnosch Difference between `i++` and `++i` has been explained "million times" all over the internet. – Daniel Langr Jan 16 '18 at 08:50
  • @DanielLangr That is true, and it is one of the reasons why StackOverflow is so appreciated, that the really good answers here trace those million explanations back to the source, i.e. the standard. – Yunnosch Jan 16 '18 at 15:23
  • @Yunnosch Agree about good answers. However, I don't think its reasonable to justify every comment with quotations or references to the Standard. – Daniel Langr Jan 16 '18 at 15:29
0

Yes, i += 1 is a shortcut for i=i+1. However, if you want to increase the value of i by 1, it is advisable to use the pre-increment operator ++i or the post-increment operator i++. These increment operators automatically increase the value of the variable by 1. Example code:

for (int i = 0; i <= 10; ++i)

The += operator is generally used to add two variables together, such as

i += j;

The above expression evaluates to

i = i + j;

Similarly, other operators can be used with the = sign, such as

i /= j; evaluates to i = i / j;

i -= j; evaluates to i = i - j;

i *= j; evaluates to i = i * j;

And so on.

aks
  • 480
  • 7
  • 14
  • This is bogus - it's perfectly normal and correct to write `i += 2;` for example; and there is no real reason to prefer `++i` over `i += 1` – M.M Jan 16 '18 at 20:18