The answer to the solution is −1. The code places a starting value of “5”. It then says that as long as the statement “value ≥ 0” is true, it will subtract 2 from the value. When executed, “5 − 2 = 3”, and “3 ≥ 0”, so this repeats. “3 − 2 = 1” and “1 ≥ 0”, so the code repeats. “1 − 2 = −1”; however, the statement “−1 ≥ 0” isn’t true, so 2 is no longer subtracted from the value. The final bit of code says to show the value, which is why the solution of −1 shows up. The “while” statement says that as long as a condition is true, something will be performed.
−Phoenix
Most excellent explanation, 15 XPs awarded for the completion.
ReplyDelete