Have you ever heard someone say "to reiterate"? Do you know what they mean? To reiterate is to repeat something.
Sometimes Algorithms contain steps that are repeated numerous times. These repeated steps are called iterations or loops.
For example the steps involved in brushing your teeth could be written as:
Squeeze a small amount of toothpaste onto toothbrush
Lift toothbrush into your mouth
Brush one tooth
Move to next tooth
Repeat steps 3 and 4 until all teeth have been brushed
Take toothbrush out of your mouth
Wash remaining toothpaste from toothbrush
When we talk about iterations in maths, we are talking about repeating something. If I say there have been 5 iterations of something, it means it has happened 5 times. The important thing is that each time the iteration happens it is building on the previous result.
Let's go through an example to make this clearer.
Say we want to write an algorithm to write the 4 times tables. We could do it like this:
Start with the number 0
Add 4 to this number
Repeat step 2 eleven more times.
If we did this I would get 0,\,4,\,8,\,12,\,16,\,20,\,24,\,28,\,32,\,36,\,40,\,44,\,48. This is 12 iterations of add 4.
Another word that is often used to describe an iteration is a loop. If you loop through something then you are repeating it.
This is a really great way to make use of a computer - if I asked you to keep repeating the same task over and over again you would quickly get bored, right? Well a computer won't ever get bored.
Nadia follows an algorithm to eat her breakfast.
Place bowl and spoon on the table
Pour cereal into the bowl
Pour milk into the bowl
Spoon a mouth of cereal into your mouth
Repeat step 4 until the cereal is finished
Place bowl and spoon in the sink
Does this algorithm contain a loop?
Which two steps form the loop?
Follow the algorithm below to fill in the results.
Step | Instruction | Result |
---|---|---|
1 | \text{Start with the number $29$} | ⬚ |
2 | \text{Subtract $1$ from this number} | ⬚ |
3 | \text{Repeat step $2$ three more times} | ⬚\\⬚\\⬚ |
4 | \text{Double the number} | ⬚ |
Iteration means to repeat a step in an algorithm.