KnowledgeBoat Logo

Computer Applications

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

Java Iterative Stmts

4 Likes

Answer

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.

Answered By

2 Likes


Related Questions