IterationIteration in practice: Cleaning teeth

When designing algorithms, there may be some steps that need repeating. This is known as iteration, and can be displayed in pseudocode or flow diagrams.

Part ofComputer ScienceAlgorithms

Iteration in practice: Cleaning teeth

A simple algorithm can be created for cleaning teeth. Suppose a person has ten top teeth. To make sure that every one of the top teeth is cleaned, the algorithm would look something like this:

  1. put toothpaste on toothbrush
  2. use toothbrush to clean tooth 1
  3. use toothbrush to clean tooth 2
  4. use toothbrush to clean tooth 3
  5. use toothbrush to clean tooth 4
  6. use toothbrush to clean tooth 5
  7. use toothbrush to clean tooth 6
  8. use toothbrush to clean tooth 7
  9. use toothbrush to clean tooth 8
  10. use toothbrush to clean tooth 9
  11. use toothbrush to clean tooth 10
  12. rinse toothbrush

Steps 2 through to 11 are essentially the same step repeated, just cleaning a different tooth every time. Iteration can be used to greatly simplify the algorithm. Look at this alternative:

  1. put toothpaste on toothbrush
  2. use toothbrush to clean a tooth
  3. move onto next tooth
  4. repeat steps 2 and 3 until all teeth are clean
  5. rinse toothbrush
Brushing your teeth can be broken down into multiple steps. Pick up your toothbrush, put toothpaste onto brush, brush teeth, rinse mouth, rinse toothbrush and put tooth brush back.
Figure caption,
The task of cleaning teeth is iteration in practice

This algorithm is much simpler. However, there is a problem – how do we know when all teeth are clean (step 4)? A is needed to solve this problem.