0

Java challenge

Hello there! Someone could tell me how 52 the output of this code? int x = 0; int y = 0; for (int z = 1; z < 5; z++) { if (( ++x > 2) && (++y > 1)) { x++; } } System.out.println(x +""+ y); I see how x increasing with x++, but I can't understand how y works. I am kind of new at Sololearn. Any chance that are the challenges have wrong solution?

18th May 2021, 10:39 PM
CEORF
CEORF - avatar
3 ответов
+ 2
CEORF a > 1 && b > 1 b will only execute when a is true So in case of your problem y will not change until ++x is false If there was or ( || ) then y will execute when ++x is false but in case of ++x true y will not change.
19th May 2021, 4:59 AM
A͢J
A͢J - avatar
+ 1
For z=1 x=1 but x is not greater than 2 which renders the whole expression as false , i. e. It doesn't proceeds to check for 2nd statement(++y>1) . For z=2 x=2 , x>2 is false. For z=3 x=3, x>2 is true . y=1, x>2 and y>1 is false. For z=4 x=4, x>2 is true . y=2, x>2 and y>1 is true and x=5 Final value for x=5 and y=2.
18th May 2021, 10:50 PM
Abhay
Abhay - avatar
+ 1
So y stay 0 while the statement before it (x>2) doesn't get true. Thank you!
18th May 2021, 11:21 PM
CEORF
CEORF - avatar