KnowledgeBoat Logo

Computer Applications

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);

Java Iterative Stmts

5 Likes

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.

Answered By

3 Likes


Related Questions