The following function counts down starting at Y. What number does the countdown return if it is called with Y= 10?

A.   -1
B.   0
C.   1
D.   10

1 Answer

DEMETRIOS LAMBROPOULOS

Updated on December 30th, 2020

do-while loops are considered post-test loops because they always execute the do-section before checking whether a specific condition is met. do-while loops are used when you always want the body of the loop to execute at least once, regardless of whether a condition is met. 

To answer this, lets step through this function as follows:

Y = 10

Enter the do-while loop

Y = 10 - 1 = 9

We then check the condition, is Y > 0? In this case, 9 is greater than zero so we loop back to the do-statement.

Y = 9 - 1 = 8

8 is greater than zero so we loop back to the do-statement.

Y = 8 - 1 = 7

7 is greater than zero so we loop back to the do-statement.

Y = 7 - 1 = 6

6 is greater than zero so we loop back to the do-statement.

Y = 6 - 1 = 5

5 is greater than zero so we loop back to the do-statement.

Y = 5 - 1 = 4

4 is greater than zero so we loop back to the do-statement.

Y = 4 - 1 = 3

3 is greater than zero so we loop back to the do-statement.

Y = 3 - 1 = 2

2 is greater than zero so we loop back to the do-statement.

Y = 2 - 1 = 1 

1 is greater than zero so we loop back to the do-statement.

Y = 1 - 1 = 0

0 is not greater than zero so we break out of the do-while loop and return Y. Since Y = 0, our answer is B.

Copyright © 2024 Savvy Engineer