What's another way to think about the increment and decrement operators? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

10th Apr 2017, 11:54 AM
Toni He
Toni He - avatar
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
10th Apr 2017, 12:28 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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 😁
10th Apr 2017, 12:00 PM
Leon lit
Leon lit - avatar