Coding and Algorithms

Ontario 09 Academic (MPM1D)

Iterations

Lesson

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 math, 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.

$4$4 times table:

I can write an algorithm for writing down the four times table like this:

- Start with the number $0$0
- Add $4$4 to this number
- Repeat step 2 eleven more times.

If we did this I would get $0$0, $4$4, $8$8, $12$12, $16$16, $20$20 , $24$24, $28$28, $32$32, $36$36, $40$40, $44$44, $48$48. This is $12$12 iterations of add $4$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?

No

AYes

BNo

AYes

BWhich two steps form the loop? Write both steps on the same line, separated by a comma.

Follow the algorithm below.

Step Instruction Result $1$1 Start with the number $29$29 $\editable{}$ $2$2 Subtract $1$1 from this number $\editable{}$ $3$3 Repeat step $2$2 three more times $\editable{}$ $\editable{}$ $\editable{}$ $4$4 Double the number $\editable{}$