ICSE Class 10 Computer Applications
Question 28 of 30
Solved Sample Paper 1 — Question 28
Back to all questions 28
Question Question 2(viii)
How many times will the following loop execute?
int x = 2, y = 50;
do
{
++x;
y -= x++;
} while (x <= 10);
return y;The given loop will execute 5 times.
Explanation
| Iteration | x | y | Remarks |
|---|---|---|---|
| 2 | 50 | Initial values | |
| 1 | 4 | 47 | ++x ⟹ x = 3 y = y - x++ ⟹ y = 50 - 3 ⟹ y = 47 x = 4 |
| 2 | 6 | 42 | ++x ⟹ x = 5 y = y - x++ ⟹ y = 47 - 5 ⟹ y = 42 x = 6 |
| 3 | 8 | 35 | ++x ⟹ x = 7 y = y - x++ ⟹ y = 42 - 7 ⟹ y = 35 x = 8 |
| 4 | 10 | 26 | ++x ⟹ x = 9 y = y - x++ ⟹ y = 35 - 9 ⟹ y = 26 x = 10 |
| 5 | 12 | 16 | ++x ⟹ x = 11 y = y - x++ ⟹ y = 26 - 11 ⟹ y = 15 x = 12 Loop terminates |