2. Find the output
class Test4 {
public static void main(String[] args)
{ int j=50;
while(true)
{
if(j<10)
break;
j=j-10;
}
System.out.println("j is "+j);
}
}
Answers
ANSWER:
J is 0.
Explanation:
because the value of j is 50 which is greater than 10.
HOPE MY ANSWER HAVE HELPED YOU.
THANK YOU.
Answer: j is 0
Explanation:
the while(true) loop moves for infinite times unless it is broken, since the condition in the loop is always true. In this case, the control cannot exit the loop until the value of j is less than 10 since this condition is required to break the loop. So the value of j keeps decreasing by 10 until it is less than 10 when the loop breaks. Let me publish the iterations in a sequential manner to make it easier.
1st iteration:
j = 50, which is not less than 10
j decreases to 40
2nd iteration:
j = 40, which is not less than 10
j decreases to 30
3rd iteration:
j = 30, which is not less than 10
j decreases to 20
4th iteration:
j = 20, which is not less than 10
j decreases to 10
5th iteration:
j = 10, which is not less than 10
j decreases to 0
6th iteration:
j = 0, which is less than 10
the control exits the loop on encountering a break statement and goes on to print 'j is 0'
Hope this helps!