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
Define a class to accept a number from user and check if it is an EvenPal number or not.
(The number is said to be EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse) and sum of its digits is an even number.)
Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even numberHow 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; }