0

Can anybody explain why x+=y-- is 16 not 15?? Here's the question: Int x=8; Int y=7; x++; x+=y--

Please...

27th Nov 2018, 3:44 PM
Konybud
Konybud - avatar
9 odpowiedzi
+ 4
For example x=10 y=6 x+=y-- Post decrement operator, will execute the operation first before decrease its value, that means 10+=6-- Is still 16, but after the operation ends y (in this case is 6) change to 5.
27th Nov 2018, 3:52 PM
Taste
Taste - avatar
+ 9
● precedence of addition assignment operator is less than post decrement operator so it will be evaulated later ● associativity of post decrement operator is from left to right & associativity of addition assignment operator is from right to left int x=8; int y=7; x++; //x is 9 now x+=y-- //x = 9 + 7 //y is 6 now & x will be 16
27th Nov 2018, 4:05 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
Here's the question: Int x=8; Int y=7; x++; x+=y--
27th Nov 2018, 3:52 PM
Konybud
Konybud - avatar
+ 2
Resultant value will be 16 as In line 3 X incremented to value 9. And in line 4 we are using post decrement operator with y which will add value of y ,which is 7 first and then make it 6 . Hence resultant value will be 9+7=16. If we print y on the next line then we will see it's value as 6.
28th Nov 2018, 4:34 AM
Dharm Vashisth
+ 1
What is the value of x after the following code? int x = 8; int y = 7; x++; //x+1 x+= y--; //x=x+1+y-1; //x=8+1+7-1 //x++ //y is 6 and x is 16
18th Sep 2022, 8:11 PM
Krishan Roy
0
Thank you! This help me a lot!
27th Nov 2018, 4:18 PM
Konybud
Konybud - avatar
0
I think ... a=1o; b=2; a++ //a=10 post increment int c=a+b; //here a=11 and b=2 so is c=13 BUT if ++a; //here a=11 Pre-increment
5th Aug 2020, 2:30 PM
Pawan Jadhav
0
I had the same issue, perhaps this is what I missed? What is the value of x after the following code? int x = 8; int y = 7; x++; x+= y--; //x= (x++; x+=y) or 8+1->9+7 [(--) concerning only the value of y AFTER equation] //y= (y--) or 8-1=7 The key point is 'VALUE OF X' ----'AFTER CODE' I hope i got that right
6th Aug 2020, 8:16 AM
Joseph
0
Explain this program What is the value of x after the following code? int x = 8; int y = 7; x++; x+= y--;
2nd Apr 2022, 2:01 PM
JANARTHANAN A