ICSE Class 10 Computer Applications
Question 22 of 30
Solved Sample Paper 3 — Question 22
Back to all questions 22
Question Question 2(ii)
Observe the following code and find that how many times will the loop execute?
int sum = 0, score = 0;
double t;
do
{
score = score + 1;
sum = sum + score;
} while(score <= 3);
t = sum / 3;The loop will execute 4 times.
Explanation
| Iteration | score | sum | Remarks |
|---|---|---|---|
| 0 | 0 | score is declared as 0 | |
| 1 | 1 | 1 | score = 0 + 1 = 1 sum = 0 + 1 = 1 |
| 2 | 2 | 3 | score = 1 + 1 = 2 sum = 1 + 2 = 3 |
| 3 | 3 | 6 | score = 2 + 1 = 3 sum = 3 + 3 = 6 |
| 4 | 4 | 10 | score = 3 + 1 = 4 sum = 6 + 4 = 10 Test condition becomes false. Loop terminates. |