Concepts / While Loops and Sentinel Values

While Loops and Sentinel Values

Collect all numbers from the user into a list using a while loop that terminates when the user enters a sentinel value like 'done'.

  • Programming

The Collection Problem

Suppose a program must collect several numbers, but it does not know in advance how many numbers the user will enter. A while loop can keep accepting values while the user continues to provide numbers. A sentinel value such as done gives the user a clear way to signal that no more numbers should be collected.

A sentinel value is a special input used to indicate that a repeated process should stop. In this pattern, done is the sentinel, while the other inputs are numeric values that belong in the list.

value receivedyessentinel checkyesUser inputNumberAppend numberdoneLoop ends
What happens after each user input, and how does entering done stop the loop without adding another number?

Tracing List Growth

Before analyzing the data, the program builds the list one number at a time. Consider the input sequence 6, 2, 9, 3, 5, then done. Each number is converted to a numeric type and appended. The sentinel is recognized as the stopping signal and is not appended.

InputActionList after the action
6Convert and append[6]
2Convert and append[6, 2]
9Convert and append[6, 2, 9]
3Convert and append[6, 2, 9, 3]
5Convert and append[6, 2, 9, 3, 5]
doneStop without appending[6, 2, 9, 3, 5]

The list changes only for numeric inputs; it remains complete when the sentinel ends the loop.

62935sentinelList[]List[6]List[6, 2]List[6, 2, 9]List[6, 2, 9, 3]List[6, 2, 9, 3, 5]done
How does the list change after each number is entered, and what does it contain immediately before the loop ends?

Analyzing the Completed List

The loop has one job: collect the numeric data. The max() and min() functions have a different job: analyze that data. They should be called after the loop exits, when the list is complete. For the collected values [6, 2, 9, 3, 5], max() finds 9 and min() finds 2.

Finding the Largest and Smallest Values

Determine the maximum and minimum values after the inputs 6, 2, 9, 3, 5, and done.

Complete collection: The sentinel ends collection, leaving the numeric list [6, 2, 9, 3, 5].

Find the maximum: Apply max() to the completed list. The largest value is 9.

Find the minimum: Apply min() to the completed list. The smallest value is 2.

The maximum is 9 and the minimum is 2.

repeated inputloop exitsanalyzeanalyzeCollect numbersdoneCompleted list[6, 2, 9, 3, 5]max()9min()2
When are max() and min() called, and how do they use the completed list?

Keeping the Sentinel Separate

The program must distinguish the sentinel from numeric input before adding anything to the list. A numeric input is converted to an int or float and then appended. The input done is handled as the stopping signal instead, so it never becomes part of the numeric data.

inspectyesappenddoneInputNumeric valueConvert to numberNumeric listdone
How does the program distinguish the sentinel value done from numbers, and why should done not appear in the final list?

Debugging the Pattern

  • Calling max() or min() inside the loop

    The list is not complete during collection, so the analysis is happening before all numbers have been gathered.

    Fix: Wait until the sentinel ends the loop, then call max() and min() on the completed list.

  • Forgetting to convert input to a number

    The list is supposed to contain numbers collected from the user.

    Fix: Convert each numeric input to a numeric type before appending it.

  • Adding the sentinel to the list

    The sentinel is a stopping signal, not one of the numbers to analyze.

    Fix: Recognize done before the append step and end the loop without adding it.

exclude sentinelconvert before appendCollected list[6, 2, done]Numeric list[6, 2]Input valuenumeric input not convertedConverted valueint or float
What changes when the list is empty, the sentinel is stored, or numeric input remains unconverted?

Practice the Trace

EASY

Trace the list for the input sequence 4, 11, 7, done. Write the list after each numeric input, identify the list immediately after done is entered, and state which two functions should be called after the loop exits.

Hints
  • Append only the numeric inputs.
  • The sentinel ends collection but does not change the list.
  • The two analysis functions are max() and min().

Practice Check

Review the input sequence 4, 11, 7, done.

After 4: The list contains [4].

After 11: The list contains [4, 11].

After 7: The list contains [4, 11, 7].

After done: The loop ends and the list remains [4, 11, 7].

Call max() and min() after the loop exits, using the completed list.

Key Takeaways

  1. Use a while loop to keep collecting numbers until the user enters a sentinel such as done.
  2. Convert each numeric input to an int or float before appending it to the list.
  3. Never append the sentinel value to the numeric list.
  4. Call max() and min() after the loop exits, when the list is complete.
  5. When debugging, check the timing of the function calls, the numeric conversion, the sentinel handling, and whether the list contains data.

Key Takeaways

  • A sentinel value ends repeated input without becoming part of the collected data.
  • The list grows only when a numeric value is converted and appended.
  • The completed list should be analyzed after the while loop exits.
  • max() finds the largest value and min() finds the smallest value in the completed list.
  • Common errors involve premature analysis, missing numeric conversion, and storing the sentinel.