Computer Applications
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;
Java Iterative Stmts
5 Likes
Answer
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. |
Answered By
4 Likes
Related Questions
Correct statement to declare an integer array of 10 elements.
- int[ ] arr = new int[10];
- int arr;
- int arr (10);
- int ( ) arr = new int (10);
Write the values of r and s after the execution of the following code.
int p = 11, q = 21, r, s; r = ++q; s = p++; r++;Write the Java statement for the following mathematical expression
If a = 8, find the value of a -= ++a + a++ + 4 - a + 6.