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
Convert the following for loop statement into the corresponding while loop and do-while loop:
int sum = 0; for (int i= 0; i <= 100; i++) sum = sum + i;
How many times are the following loop bodies repeated? What is the final output in each case?
int z = 1; while (z < 10) if((z++) % 2 == 0) System.out.println(z);
What are the differences between while loop and do-while loop?
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++);