I'm having trouble understanding how Post Increment (++
), Pre Increment (--
) and addition/subtraction work together in an example.
x++
means add 1 to the variable.
x--
means subtract 1 from the variable.
But I am confused with this example:
int x = 2, y = 3, z = 1;`
y++ + z-- + x++;
I assume this means 3(+1) + 1(-1) + 2(+1)
Which means the result should be 7.
But when I compile it, I get 6
. I don't understand.
int main() {
int x=2, y=3, z=1;
int result;
result = y++ + z-- + x++; //this returns 6
cout << result << endl;
return 0;
}