0

# What's another way to think about the increment and decrement operators?

I'm having some trouble understanding the difference between ++X and X++. Say that X=0. I know that in X++, X = 0 becomes X = 1. But what about ++X? Is it still X=1? And what if there's an equation that follows. Say X=0 Y=5, Y=+ ++X; What would be the answer?

2 Answers

+ 11

I'm not sure if this saying is correct for all cases... But here goes the simple method:
For ++X, whatever you do, you increment value of X first, before doing other computations.
For X++, whatever you do, you do first, before incrementing value of X.
------
E.g.
int X = 0;
cout << X++;
// prints X, and then X become 1
// outputs 0
------
int X = 0;
cout << ++X;
// X become 1, and then prints X
// outputs 1

+ 6

here's a simple example :
int x =0;
//here we use the last value that is assigned to x and that's 0 then increase it to 1.
cout << x++ << endl; //0
//here we increase the value of x first, then print it out .
cout << ++x << endl; //2
hope this help đ