Abhisek Ohdedar wrote:Hi,
i am taking preparations for SCJP and of course the book written by Sierra-Bates is my guideline
.
I was going through mock SCJP questions on the net and came across these.
Question 2.
Given the code. What is the result?
int i = 10;
while (i++ <= 10) {
i++;
}
System.out.print(i);
Here answer given is 13.
But i think the answer should be 12.
Can anyone please explain the answer?
It seems I can only provide answer to Question 2.
Ok, let's look at it together:
means the
<= condition is tested before the incrementing takes place.
So, the condition is true then
i becomes 11
B'cos the condition is true, the inner
i++; turns
i to 12
Now to the main issue
The system returns to continue with the while loop, tests the condition, discovers it's false, increments
i, then leaves the loop to print out the value of
i
Thereby making
i = 13!