 # 12.05 Iterations

Lesson

## Ideas

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.

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

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.

Idea summary

Iteration means to repeat steps in an algorithm.

### Outcomes

#### VCMNA194

Follow a mathematical algorithm involving branching and repetition (iteration)