Computer Applications
Analyze the following program segment and determine how many times the loop will be executed and what will be the output of the program segment?
int p = 200;
while(true){
if(p < 100)
break;
p = p - 20;
}
System.out.println(p);
Java
Java Iterative Stmts
ICSE 2011
64 Likes
Answer
80
The loop executes 7 times
Working
| p | Remarks |
|---|---|
| 200 | 1st Iteration |
| 180 | 2nd Iteration |
| 160 | 3rd Iteration |
| 140 | 4th Iteration |
| 120 | 5th Iteration |
| 100 | 6th Iteration. |
| 80 | 7th Iteration. Now p < 100 becomes true so break statement is executed terminating the loop. |
Answered By
34 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++)
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; }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