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