KnowledgeBoat Logo
|

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

pRemarks
2001st Iteration
1802nd Iteration
1603rd Iteration
1404th Iteration
1205th Iteration
1006th Iteration.
807th Iteration. Now p < 100 becomes true so break statement is executed terminating the loop.

Answered By

34 Likes


Related Questions