+ 1

Java code

im confused about x++ and ++x and i need explanation of this code https://code.sololearn.com/c3BFPNrretEU/?ref=app

8th Jun 2019, 3:01 AM
Tzion
Tzion - avatar
3 Answers
+ 1
Alright so... X = 1 Y = 2 First loop prints x++ + ++y; x is still 1, y becomes 3 (pre-increment) = total 4. After the loop ends, x becomes 2 2++ + ++3 x is still 2, y becomes 4 = total 6. After this loop ends, x become 3 3++ + ++4 x is still 3, y becomes 5 = total 8 After this loop ends, x becomes 4 4 is bigger than 3 so loop ends with BREAK; https://www.sololearn.com/learn/Java/2141/?ref=app
8th Jun 2019, 3:04 AM
HNNX 🐿
HNNX 🐿 - avatar
+ 1
it is like y=1+y; //++y before calculate print( x+y ); x=x+1 // x++ after calculate x=1 y=2 | x=1 ++y=3 | 1+3 = 4 | x++=2 y=3 x=2 y=3 | x=2 ++y=4 | 2+4 = 6 | x++=3 y=4 x=3 y=4 | x=3 ++y=5 | 3+5 = 8 | x++=4 y=5 break
8th Jun 2019, 3:47 AM
zemiak
+ 1
zemiak Anhjje 🐑 Thank you so much! i finally knew it.!
8th Jun 2019, 5:47 AM
Tzion
Tzion - avatar