- 1
Why is the output 0 ?
int x = 5; System.out.println(++x - x--);
7 Réponses
+ 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.
+ 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.
+ 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.
+ 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
+ 1
Harsh That makes sense
+ 1
Jayakrishna🇮🇳 Also thanks
0
Jayakrishna🇮🇳 Why does x-- equal 6 ?