ICSE Class 9 Computer Applications
Question 8 of 29
Iterative Constructs in Java — Question 11
Back to all questions 11
Question Question 8(ii)
How many times are the following loop bodies repeated? What is the final output in each case?
int y = 1;
while (y < 10)
if (y % 2 == 0)
System.out.println(y++);The loop repeats for infinite times.
Output
The given code gives no output.
Explanation
The value of y is 1. The test condition of while loop — y < 10 is true but the test condition of if — y % 2 == 0 is false. Thus, the control does not enter the if statement and the value of y remains 1 infinitely. Thus, the while loop continues infinitely.