What is the output of the following Java code?
count = 1;
y = 10;
while (count < 10)
{
y = y - 1;
count++;
}
System.out.println("y = " + y + " and count = " + count);
1
Expert's answer
2020-12-08T15:11:23-0500
Output:
y = 1 and count = 10
As 'count' increments by 1 every iteration, the cycle will finish when it hits 10. As the initial value is 1, the number of iterations is 9. So 'y' will decrement 9 times, and the final value will be 10 - 9 = 1.
Comments
Leave a comment