+ 9

Find the result

find the value of x : int y=10; int x=++y*(y++ +5); I expected the value of x to be 180.((10+5)*12). It is showing 176.(11*16).

26th Aug 2018, 3:50 AM
Navoneel
6 Réponses
+ 9
yes it is right java compiler works from left to right for statement x=++y (y++ +5); It will first increment the statement and convert it into value form as x = 11 (11+5); x = 11*(16); x = 176
26th Aug 2018, 4:19 PM
Purab Gupta
Purab Gupta - avatar
+ 8
Meet Mehta , shouldn't the part inside the parenthesis be operated first. They have the highest order in operator precedence.
26th Aug 2018, 4:19 AM
Navoneel
+ 8
thanks to everyone
26th Aug 2018, 4:48 PM
Navoneel
+ 4
the simple thing is the post fix and prefix operators will be from left to right. simply run these type of questions and get the answer this and only this pattern is followed. 😊😉 thnx
26th Aug 2018, 7:22 AM
Aahnik Daw
Aahnik Daw - avatar
+ 3
yeah it shows right... when u use pre increment variable will increment first and then take part in calculation and in post increment it first take part in oprration with its initial value and then increment after operation ends... so here y is incremented first to 11 11*(11+5) 11*16=176 and after that y value increment to 12
26th Aug 2018, 10:16 AM
Aarav Raj
Aarav Raj - avatar
+ 1
y=10 x = ++y (11 pre increment) *(y++ (11 post increment) + 5). x = 11 * (11 + 5). =11*16 =176. Hope this helps ☺️☺️.
26th Aug 2018, 4:10 AM
Meet Mehta
Meet Mehta - avatar