ICSE Class 10 Computer Applications
Question 12 of 69
Iterative Constructs in Java — Question 12
Back to all questions 12
Question Question 8
What is the output produced by the following code?
int n = 20;
do {
System.out.println(n);
n = n - 3;
} while (n > 0);Output
20
17
14
11
8
5
2
Explanation
The initial value of n is 20. Every time the do-while loop is executed, n decreases by 3. Execution of the loop is summarized in the table below:
| Iteration | n | Output | Test Conditionn > 0 | Remarks |
|---|---|---|---|---|
| 1st | 20 | 20 | True | n is initially 20. After updation, it becomes 17 (20 - 3) |
| 2nd | 17 | 17 | True | n becomes 14 (17 - 3) |
| 3rd | 14 | 14 | True | n becomes 11 (14 - 3) |
| 4th | 11 | 11 | True | n becomes 8 (11 - 3) |
| 5th | 8 | 8 | True | n becomes 5 (8 - 3) |
| 6th | 5 | 5 | True | n becomes 2 (5 - 3) |
| 7th | 2 | 2 | True | n becomes -1 (2 - 3) |
Since, the condition of while loop becomes false, the loop terminates.