Why is the output 0 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Why is the output 0 ?

int x = 5; System.out.println(++x - x--);

11th Jul 2020, 5:03 PM
Timmy
7 Answers
+ 5
Timmy First we evaluate ++x ++x for x=5 will first return the value of x (that is 5 till now ) and then increase the value of x to 6. Now x-- x-- will decrease the value of x from 6 to 5 and then return the value of x that is 5 So ++x will return 5 and then x-- will also return 5. So ++x - x-- will return 0 (zero). And Harsh that is wrong what you have given as answer. I hope you understand. For any querry mention me. Thank you.
11th Jul 2020, 5:17 PM
Akshay Panwar
Akshay Panwar - avatar
+ 10
int x=5; System.out.println(++x-x--); Here first always preincrement will be solve so ++x first so the value of x will be 6 and present value of x will be 6 not 5 so answer will be 0 after that post decrement.
11th Jul 2020, 6:36 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
x=5; ++x - x-- //in this ++x =>x=6 6-6=0. // after this x-- is evaluted so again x-- =>x=5 ++x is (pre-increment/decrement operators) first evaluted then used in expression. x-- is (post increment/decrement operator) is first used in expression, next value evaluated.
11th Jul 2020, 5:05 PM
Jayakrishna 🇮🇳
+ 1
Read about post fix operator. Refresh page. I already added explanation... And read this: Edit: Timmy See in this increment topic's post/pre incrementation para: And an extra link for more examples.. @Harsh that is, according you 6- -6= 6+6=12... Check it.(deleted) https://www.sololearn.com/learn/CPlusPlus/1610/?ref=app https://www.sololearn.com/Discuss/2380608/?ref=app
11th Jul 2020, 5:10 PM
Jayakrishna 🇮🇳
+ 1
Harsh That makes sense
11th Jul 2020, 5:10 PM
Timmy
+ 1
11th Jul 2020, 5:10 PM
Timmy
0
Jayakrishna🇮🇳 Why does x-- equal 6 ?
11th Jul 2020, 5:07 PM
Timmy