topic badge

14.03 Iterations

Lesson

Are you ready?

Can you identify branching in an algorithm?

Examples

Example 1

Follow this rounding algorithm for the number 9.7:

  • If the digit after the decimal point is 5 or more than 5, round up to the next whole number.

  • If the digit after the decimal point is less than 5, write the number without the decimal value.

Worked Solution
Create a strategy

Look at the number after the decimal point to decide which condition to follow.

Apply the idea

The number after the decimal point in 9.7 is 7, which is more than 5.

So, we need to follow the first condition and round up to the next whole number which is 10.

Idea summary

The key word to look for with a branch is if.

Iterations

This video shows us what a loop (or iteration) is, and why they are used in code.

Loading video...

Examples

Example 2

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 3

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 steps in an algorithm.

Outcomes

VCMNA221

Design algorithms involving branching and iteration to solve specific classes of mathematical problems

What is Mathspace

About Mathspace