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;
Answer

The loop will execute 4 times.

Explanation
IterationscoresumRemarks
 00score is declared as 0
111score = 0 + 1 = 1
sum = 0 + 1 = 1
223score = 1 + 1 = 2
sum = 1 + 2 = 3
336score = 2 + 1 = 3
sum = 3 + 3 = 6
4410score = 3 + 1 = 4
sum = 6 + 4 = 10
Test condition becomes false. Loop terminates.