How was increment equal to decrement here?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How was increment equal to decrement here??

In this code: int main() { int x=5,y=10,sum; x-- ; sum=x+y; cout<<sum<<"\n"; cout<<x<<"\n"; //How come final result as 14 not 15? return 0; How come 14 not 15?

27th Oct 2020, 2:49 PM
eyad omar
eyad omar - avatar
3 Answers
+ 1
x-- is equivalent to x = x - 1; so after that statement x becomes 4.
27th Oct 2020, 2:55 PM
QTWizard
+ 1
You can just testing x value before displaying sum value with cout then you can understand why. x--; cout<<x<<endl; sum = x+y; cout<<sum<<"\n";
27th Oct 2020, 3:08 PM
HBhZ_C
HBhZ_C - avatar
0
You decrement x and then you add x to sum, if you want sum to be 15, you have to do "sum = x + y" before "x--".
27th Oct 2020, 5:43 PM
Admi