came across this code and tried to solve it ... but do not quite understand why it prints out 63?
public static void main(String[]args)
{
int x = 0;
int y = 0;
for(int z= 0; z < 5; z++)
{
if ((++x > 2)&&(++y > 2))
{
x++;
}
}
System.out.println(x+""+y);
}
First of all, ++x means x is being added 1, and afterwards read for the check in the if statement. So x is 1 the first time you check it.
But there is another thing in Java, if you check two statements, like you do in the following:
if ((++x > 2)&&(++y > 2))
If the first check fails, the second doesn not get executed.
I written the output in console, having ----- for all for loop cycles:
x: 0 y: 0 z: 0 if: (false && (not executed) ) x: 1 ----- x: 1 y: 0 z: 1 if: (false && (not executed) ) x: 2 ----- x: 2 y: 0 z: 2 if: (true && false) x: 3 y: 1 ----- x: 3 y: 1 z: 3 if: (true && false) x: 4 y: 2 ----- x: 4 y: 2 z: 4 if: (true && true) x: 5 y: 3 x: 6 ----- 63
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.