KnowledgeBoat Logo

Computer Applications

How many times will the following loop execute? What value will be returned?

int x = 2;
int y = 50;
do{
++x;
y -= x++;
}
while(x <= 10);
return y;

Java Iterative Stmts

ICSE Sp 2024

115 Likes

Answer

The loop will execute 5 times and the value returned is 15.

Working

IterationXYRemark
 250Initial values
1347x = 4 (3 + 1), y = 47 (50 - 3)
2542x = 6 (5 + 1), y = 42 (47 - 5)
3735x = 8 (7 + 1), y = 35 (42 - 7)
4926x = 10 (8 + 1), y = 26 (35 - 9)
51115x = 12 (11 + 1), y = 15 (26 - 11)
 1215Condition becomes false. Loop terminates.

Answered By

63 Likes


Related Questions