0

I can not grasp the concept as to how these statements produce different values. As far as I know x+=1, means x = x + 1. I also know that x++ should be equivalent to x + 1.

I've also searched this topic and found posts which ask the same question, those posts are answered usually by stating the statements/expressions are the same, but the different result was due to another code mistake. With the example I will provide I don't see how there is a code mistake so please explain, thank you.

int x = 0;
x++;

x should be 1 at this point because x++ adds 1 to x.

So why is it that if I assign x to 0, and then proceed to code "cout << x++;" I get a value of 0 on the screen?!. How does x++ become 0 if x++ is equal to x+1 and if x is 0 then 1+0=1? I've been told its due to ++ being put after x, but why does that matter if dealing with addition 1 + 0 is the same as 0 + 1?

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
bob
  • 33
  • 1
  • 2
  • 3
  • You put the `++` after the `x`, which means that you want the increment to occur after the value of the expression is determined. If you want the increment to occur first, use `++x`. C++ is not math. Symbols do not have to mean in C++ what they mean in mathematics. – David Schwartz Jun 11 '16 at 08:20
  • Voting to re-open. The question linked to rationalize why this post was closed, i.e. about post and pre increments (`i++` & `++i`) does not include any discussion explaining the differences between the following expressions and statement: `x+=1` `x++` and `x = x +1` . These _are not_ duplicate questions by any means. – ryyker Feb 13 '20 at 14:42

6 Answers6

4

cout << x++; outputs the value of x before the increment as you are using the postfix increment operator.

cout << ++x; would do what you expect.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

There are two forms of the ++ operator: prefix and postfix. You're using the postfix form.

x++ returns the current value of x, then increments it. When you use cout << x++, it prints x then increments it.

++x does what you want: it increments x then returns it. cout << ++x will give you what you want.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
John Burger
  • 3,662
  • 1
  • 13
  • 23
0

x++ simply returns x and then increases x by one.

So cout << x++ in your example would be the equivalent of cout << x; x = x+ 1;

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Okay I understand now that cout << x++; just returns x then adds 1 to it. But if that is the case how come if I cout << x++; << x; I still get 0? Seems like 1 never got added to x. – bob Jun 11 '16 at 08:26
  • 1
    Does `cout << x++; << x;` compile? – Ed Heal Jun 11 '16 at 08:28
  • Yes it becomes "00" on my screen – bob Jun 11 '16 at 08:30
  • It does not compile - see https://ideone.com/5qzjZM – Ed Heal Jun 11 '16 at 08:32
  • it does for me, here is my code. #include using namespace std; int x = 0; int main() { cout << x++ << x; return 0; } – bob Jun 11 '16 at 08:44
  • Where is the code? – Ed Heal Jun 11 '16 at 08:44
  • @EdHeal remove the ; after x++ in "cout << x++; < – john Dec 21 '19 at 17:50
  • @bob The problem with your code is that "int x = 0" is not inside the main() function. So you have a global variable. Use the following code and you will see that it does increment properly. I included your x variable and added a y variable inside main(). " #include using namespace std; int x = 0; int main() { int y = 0; cout << x++ << x; cout << endl; cout << y++ << y; return 0; } " – john Dec 21 '19 at 18:02
  • @bob Here are some links on how to modify global variable from within main() https://overiq.com/c-programming-101/local-global-and-static-variables-in-c/ and this https://www.includehelp.com/code-snippets/c-program-to-define-modify-and-access-a-global-variable.aspx – john Dec 21 '19 at 18:04
0

That's post-increment, which means that it will execute the variable as-is, and then add to the variable.

Had you tried the pre-increment ++x, then it would add to the variable, and execute the variable as-is (which is now incremented).

Therefore, int x = 0; x = x + 1; cout << x; and int x = 0; x++; cout << x++; will all print 1.

Jossie Calderon
  • 1,393
  • 12
  • 21
0

Cout << x++ will first print x and then increase it, x++ is post increment so after the operation x is increased. Instead ++x will do the opposite. And also if you cout some expression like x+1 the expression will be evaluated before printing

0

x++ is a post increment, which means that it increments after it has run the current statement.

In contrast, ++x executes the current statement before executing the increment.

Hope this helps.

Tayab Soomro
  • 155
  • 2
  • 14