User Tools

Site Tools


cplusplus:control_structures_repetition_in_algorithms

This is an old revision of the document!


View page as slide show

Control Structures: Repetition in algorithms

Mithat Konar
2013-02-28

Introduction

  • Input validation
  • Sentinel/flag control
  • Counter control
  • Iterators

Input validation

  • “Block 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/flag control

  • Sentinel:
    • Value(s) that indicate(s) a special condition; “special value”
  • Flag:
    • Sometimes used as a synonym for sentinel
    • Sometimes means a sentinel that is a Boolean 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.
  • 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.1372583644.txt.gz · Last modified: 2013/06/30 09:14 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki