topic badge

12.07 Iterations

Lesson

Iterations

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:

  1. Squeeze a small amount of toothpaste onto toothbrush

  2. Lift toothbrush into your mouth

  3. Brush one tooth

  4. Move to next tooth

  5. Repeat steps 3 and 4 until all teeth have been brushed

  6. Take toothbrush out of your mouth

  7. 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:

  1. Start with the number 0

  2. Add 4 to this number

  3. 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.

Examples

Example 1

Nadia follows an algorithm to eat her breakfast.

  1. Place bowl and spoon on the table

  2. Pour cereal into the bowl

  3. Pour milk into the bowl

  4. Spoon a mouth of cereal into your mouth

  5. Repeat step 4 until the cereal is finished

  6. Place bowl and spoon in the sink

a

Does this algorithm contain a loop?

Worked Solution
Create a strategy

Look for a step that tells to repeat a prior step.

Apply the idea

Since step 5 tells us to "repeat" step 4, the algorithm contains a loop.

b

Which two steps form the loop?

Worked Solution
Create a strategy

Look for the steps mentioned in part (a).

Apply the idea

We knew from part (a) that step 5 tells us to repeat step 4. So steps 4 and 5 form the loop.

Example 2

Follow the algorithm below to fill in the results.

StepInstructionResult
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}
Worked Solution
Create a strategy

Follow each step of the algorithm.

Apply the idea

Step 1 tells us to start with 29, so the first result is 29.

Step 2 tells us to subtract 1 from 29, which gives us 29-1=28.

Step 3 tells us to repeat step 2 three more times. Since step 2 tells us to subtract 1, we need to subtract 1 three times to get: 27,\,26,\,25.

Step 4 tells us to double the last number found in step 3, which is 25.

\displaystyle \text{Result}\displaystyle =\displaystyle 25\times2Double the last number
\displaystyle =\displaystyle 50Evaluate
StepInstructionResult
1\text{Start with the number $29$}29
2\text{Subtract $1$ from this number}28
3\text{Repeat step $2$ three more times}27\\26\\25
4\text{Double the number}50
Idea summary

Iteration means to repeat a step in an algorithm.

What is Mathspace

About Mathspace