+ 2
[DUPLICATE] x++ and ++x?
int a=3; int b=2; b=a++; //here b assign 3+1? cout<<++b; then the output is why not 1+4? i guess the output is 5 but it's 4. plz explain this, i'm beginner and dumb at programming.
5 Answers
+ 8
++x (prefix op) -> increments i and returns the value of i after the increment
x++ (postfix op) -> increments i and returns the value of iĀ beforeĀ the increment
Here b=a++;
Here value of b is 3 and will be 4.
cout<<++b;
Here value of b is 4
+ 2
b=3 and then a becomes 4 is what b=a++; does.
+ 2
a++ only increments a, not b
+ 1
x++ means first do what you have to do then add one to the variable.
++ x means the opposite, that is, before anything else add one variable.
at first I was also confused by this (in JavaScript) but then I got used to it.
OBS: the same goes for the --.