cplusplus:control_structures_repetition_in_algorithms
This is an old revision of the document!
Control Structures: Repetition in Algorithms
Mithat Konar
2014-02-24
Introduction
- Input validation
- Sentinel/flag control
- Counter control
- Iterators
Input validation
- “Prevent moving to next section until user has entered valid data.”
- Pseudo code:
Get input value from user While input value is not valid Get input value from user
Sentinel control
- Sentinel:
- Value(s) that indicate(s) a special condition—a “special value”
Sentinel-controlled repetition
- “Repeat as long as a sentinel condition is not matched.”
- Pseudo code:
Get input value While input value is not SENTINEL value Do something Get input value
- Input validation might be considered a special case of sentinel controlled repetition.
- Flag:
- Sometimes used as a synonym for sentinel
- Sometimes means a sentinel that is a Boolean value
Menus
- Repeating menus can be implemented with sentinel control.
- Pseudo code:
Show menu Get menu choice from user While input value is not QUIT value <Process the menu choice> Show menu Get menu choice from user
Counter-controlled repetition
- “Repeat something a known number of times.”
- Uses a counter variable.
- Pseudo code:
Set counter's initial value While counter has not yet exceeded the STOP_VALUE Do something Increment counter
Iterators
- Do something to every item in a collection.
- Pseudo code:
For each item in collection Do something with or to the item
cplusplus/control_structures_repetition_in_algorithms.1393279937.txt.gz · Last modified: 2014/02/24 22:12 by mithat