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
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);
Write a program to accept n number of input integers and find out:
i. Number of positive numbers
ii. Number of negative numbers
iii. Sum of positive numbers
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?