+ 1

Why this code output is 3?

int x=2; y=2; z; if( ++×>2&&y--<2){ z=x+y; System.out.print (z)} else System.out.print(x);

12th Jul 2018, 12:36 AM
Caty Zurano
Caty Zurano - avatar
4 Answers
+ 3
That's easy. ++x = 3 that is greater than 2. but second condition is y-- that is equals to 2 and it isn't smaller than 2. So it just prints x value that is equal to 3(after ++x it becomes 3)...
12th Jul 2018, 12:43 AM
Ilias
Ilias - avatar
+ 2
--y is equal to 1, not 2
12th Jul 2018, 3:08 AM
Dlite
Dlite - avatar
+ 1
why is --y equals to 2?
12th Jul 2018, 12:59 AM
Caty Zurano
Caty Zurano - avatar
0
it says y--... first it check if y is smaller than 2(that is false ) and after decrease by one
12th Jul 2018, 9:55 AM
Ilias
Ilias - avatar