What does x++ mean exactly? The effect it has seems to be to add one to x, but I don't know if that's its only use.
Code:for ( int x = 0; x < 10; x++ ) {
etc...
Printable View
What does x++ mean exactly? The effect it has seems to be to add one to x, but I don't know if that's its only use.
Code:for ( int x = 0; x < 10; x++ ) {
etc...
That is same like x = x +1
So "++" is exactly the same as "+ 1" in all circumstances?
No, it isn't. For example, take the following statements:
andCode:int x = 1;
int a = (x = x + 1);
In both cases, x will be 2 at the end of exection, but in the first example, a will have a value of 2, because it evaluates (x=x+1) first, and then assigns the value of x to a. In the second example, b will have the value of 1, because it assigns the value of x to b, and then adds 1 to x.Code:int x = 1;
int b = x++;
This is where these make a difference.Code:int i3 = 1;
cout << i3++ << endl;
cout << i3 << endl;
i3 = 1;
cout << ++i3 << endl;
cout << i3 << endl;
More concisely(I think).
x++ does indeed mean x+1, however its the order that matters.
------
Example:
++x means, immediately increment x and use it as it was modified
x++ means, use x now and then increment it
Code:int x = 1;
int b = x++; // b = 1, x = 2 (postfix, increment after assignment)
int c = ++x; // c = 3, x = 3 (prefix, increment before assignment)
Code:int x = 10;
int b = x--; // b = 10, x = 9 (postfix, decrement after assignment)
int c = --x; // c = 8, x = 8 (prefix, decrement before assignment)
x++ is equivalent to something, it's just that it's
Note that the comma operator evaluates the expression on the left, then evaluates the expression on the right, and returns that value.Code:x++ <-> (x = x + 1, x - 1)
++x <-> (x = x + 1)
x-- <-> (x = x - 1, x + 1)
--x <-> (x = x - 1)
>So "++" is exactly the same as "+ 1" in all circumstances?
x++ and ++x have the same effect as x = x + 1...until you put them in a more complex expression. Once you do that, you can read ++x and x++ as "Oh my god! I just had demons fly out my nose!". That should protect you until you know better. ;)
>"Oh my god! I just had demons fly out my nose!"
Thanks a lot, Prelude. Milk just flew outta my nose... *cries*
I have yet to proclaim "Oh my god! I just had demons fly out my nose!" I guess I am not pre and post incrementing correctly then :).
Anyway as for the OP I think he has enough answers to repel those demons from flying out his nose for the time being.