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.
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:
put toothpaste on toothbrush
use toothbrush to clean tooth 1
use toothbrush to clean tooth 2
use toothbrush to clean tooth 3
use toothbrush to clean tooth 4
use toothbrush to clean tooth 5
use toothbrush to clean tooth 6
use toothbrush to clean tooth 7
use toothbrush to clean tooth 8
use toothbrush to clean tooth 9
use toothbrush to clean tooth 10
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:
put toothpaste on toothbrush
use toothbrush to clean a tooth
move onto next tooth
repeat steps 2 and 3 until all teeth are clean
rinse toothbrush
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 conditionIn computing, this is a statement or sum that is either true or false. A computation depends on whether a condition equates to true or false. is needed to solve this problem.