KnowledgeBoat Logo
|

Computer Applications

How many times will the following loop execute?

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

Java Iterative Stmts

29 Likes

Answer

The given loop will execute 5 times.

Explanation
IterationxyRemarks
 250Initial values
1447++x ⟹ x = 3
y = y - x++ ⟹ y = 50 - 3
⟹ y = 47
x = 4
2642++x ⟹ x = 5
y = y - x++ ⟹ y = 47 - 5
⟹ y = 42
x = 6
3835++x ⟹ x = 7
y = y - x++ ⟹ y = 42 - 7
⟹ y = 35
x = 8
41026++x ⟹ x = 9
y = y - x++ ⟹ y = 35 - 9
⟹ y = 26
x = 10
51216++x ⟹ x = 11
y = y - x++ ⟹ y = 26 - 11
⟹ y = 15
x = 12
Loop terminates

Answered By

17 Likes


Related Questions