Ask Question

Consider the following code segment. int count = 1; int value = 31; while (value > = 10) { value = value - count; count = count + 3; } System. out. println (value); What is printed as a result of executing the code segment?

+5
Answers (1)
  1. 7 April, 15:22
    0
    30

    26

    19

    9

    Explanation:

    Initially, the value of count is 1 and the value of the variable "value" is 31. Since value is greater than 10, the while loop condition holds true and the program enters the while loop and will continue to execute the code inside the loop until the value changes to less than 10. "value" is reduced in the statement "value = value - count; ". Since we know value is initially 31, and count is 1, the value after this statement is executed will become value=31-1 = 30. "count" variable is now incremented by 3, so it now becomes 1+3 = 4. The next line will print this value onto the screen. The loop run again since value is > = 10. In the second run, the value will decrement by 4 as count was incremented by 3 in the previous run. Therefore, the value would become 26, while the count is again incremented by 3 to its new value 7. In the next run, the value of the value variable is therefore decremented by 7 to its new value 19 (26-7=9), and count is again incremented by 3 to make it 10 (7+3=10). The value is decremented by 10 to its new value 9 in the next run. Since 9 is less than 10 the while condition is no longer true and the program exits the while loop.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Consider the following code segment. int count = 1; int value = 31; while (value > = 10) { value = value - count; count = count + 3; } ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers