ICSE Class 9 Computer Applications Question 7 of 29

Iterative Constructs in Java — Question 10

Back to all questions
10
Question

Question 8(i)

How many times are the following loop bodies repeated? What is the final output in each case?

int x = 1;
while (x < 10)
    if(x % 2 == 0)
        System.out.println(x);
Answer

The loop repeats for infinite times.

Output

The given code gives no output.

Explanation

The value of x is 1. The test condition of while loop — x < 10 is true but the test condition of if — x % 2 == 0 is false. In the absence of update expression, the while loop continues infinitely.