why | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why

why x - x++ == 0 ? and not 1 ? what happens with "++" ?

3rd Apr 2020, 10:59 AM
Radu Harangus
Radu Harangus - avatar
8 Answers
+ 3
Oh, no, it's not like that, because ++ has higher precedence, it is evaluated first so instead of (x - x) ++ it is (x) - (x++) see?
3rd Apr 2020, 11:15 AM
Gordon
Gordon - avatar
+ 2
Because x++ is post increment So the increment happens after the value is taken out for subtraction
3rd Apr 2020, 11:02 AM
Gordon
Gordon - avatar
+ 1
example if you have a=1 b=a++ so a=2 b=1 here x=1 1-1++ ==0 and later x = 2
3rd Apr 2020, 11:02 AM
Marco
Marco - avatar
+ 1
Yes and it is because it is x++ If it is x - ++x, the result will be -1
3rd Apr 2020, 11:05 AM
Gordon
Gordon - avatar
0
I would imagine like: x - x = 0 and then 0++ which is 1
3rd Apr 2020, 11:04 AM
Radu Harangus
Radu Harangus - avatar
0
@Gordon I understand what ++x and x++ mean and in any other expression I am able to figure it out. Also, I tried x = x - ++x, which outputs -1 and just by the same logic (to me) x = x - x++ should be 1, not 0.
3rd Apr 2020, 11:07 AM
Radu Harangus
Radu Harangus - avatar
0
@Gordon Ok, now I see, thank you very much!
3rd Apr 2020, 11:20 AM
Radu Harangus
Radu Harangus - avatar
0
It not work because you are removing the x++ from x --> x - x++ you need that x++ or x = x++ to add one not remove one
5th Apr 2020, 7:32 AM
Kenan238
Kenan238 - avatar