Final answer:
Option 3: Use a flag to control the while loop. The sum and num are variables in a program that calculates the total of user-entered numbers until a sentinel value is input, at which point the loop ends and the sum is displayed.
Step-by-step explanation:
The student's question relates to writing a program that calculates the sum of a series of numbers entered by the user until a sentinel value is entered. To accomplish this, we would need to:
- Initialize sum and num to 0.
- Prompt the user to provide an integer input.
- Use a flag or a specific sentinel value to control the while loop.
- Add each input number to sum until the sentinel value is detected.
For example, in Python:
sum = 0
while True:
num = int(input("Enter a number (999 to stop): "))
if num == 999:
break
sum += num
print("The total sum is:", sum)
In this code, the sentinel value is '999'. The loop will continue to ask the user for numbers and add them to the sum until '999' is entered.