+ 2

Int x=4; x+=(x++)+(++x)+x;System.out.println (x);

the answer will be 20 can any one explain this question

12th Feb 2017, 10:29 AM
sameer sahu
sameer sahu - avatar
6 Respuestas
+ 17
First, you have 4. Then in the second line you have this: 4 += 4 + 6 + 6 x++ means you put value of x first and then it is incremented, so you got 4. In the case of ++x the value of x is incremented once again, that's why you got 6.
12th Feb 2017, 10:57 AM
Igor Makarsky
Igor Makarsky - avatar
+ 4
20
12th Feb 2017, 10:29 AM
sameer sahu
sameer sahu - avatar
0
++x increments then assigns value, x++ assings value then increments.
12th Feb 2017, 11:06 AM
Andre van Rensburg
Andre van Rensburg - avatar
0
21 is coming 😑
11th Jun 2019, 8:44 AM
swapnil sagar
0
The answer here should be 22. Below is the reason which I can see x+=(x++)+(++x)+x which is same as x=(x + ((x++)+(++x)+x) Now with order of prioritty (x++) will be executed first. x++ will increment the value of x first and return the old value, which is 4 Next ++x will be executed ++x will increment the value first and return the new value, which 6 Now our problem is changes like below x=6 x=x+4+6+x Which is 22! However when I execute x=x + ((x++)+(++x)+x then I am getting 21. I am yet to figure out the reson why removal of bracket changes the execution
24th Sep 2020, 3:33 AM
Jaison Sebastain
Jaison Sebastain - avatar
0
Var x = 4 X *= 2 X -=3 X++ Println(x)
13th May 2021, 11:15 AM
HaRshul Patidar
HaRshul Patidar - avatar