Computer Applications
Give the output of the following program segment and also mention the number of times the loop is executed:
int x, y;
for (x = 9, y = 4; x <= 45; x+=9) {
if (x % y == 0)
break;
}
System.out.println(x);
Java
Java Iterative Stmts
25 Likes
Answer
36
Loop executes 4 times
Working
Below table shows the dry run of the loop:
| x | y | Remarks |
|---|---|---|
| 9 | 4 | 1st Iteration |
| 18 | 4 | 2nd Iteration |
| 27 | 4 | 3rd Iteration |
| 36 | 4 | 4th Iteration. As 36 % 4 is 0 so break statement terminates the loop. |
Answered By
16 Likes
Related Questions
To execute a loop 10 times, which of the following is correct?
- for (int i=11;i<=30;i+=2)
- for (int i=11;i<=30;i+=3)
- for (int i=11;i<20;i++)
- for (int i=11;i<=21;i++)
Which of the following are entry controlled loops?
(a) for
(b) while
(c) do..while
(d) switch
- only a
- a and b
- a and c
- c and d
How many times will the following loop execute? Write the output of the code:
int x=10; while (true){ System.out.println(x++ * 2); if(x%3==0) break; }