topic badge

11.10 Loops

Lesson

Ideas

Loops

Loops (or  iterations  ) are steps that are repeated a number of times in an algorithm. They make algorithms simpler because they don’t need to include groups of unnecessary steps.

For example, the steps involved in eating breakfast could be:

  1. Fill bowl with cereal

  2. Pour milk onto cereal

  3. Place spoon in bowl and scoop up cereal and milk

  4. Lift spoon to mouth and empty contents of the spoon into mouth

  5. Eat spoonful of cereal and milk

  6. Place spoon in bowl and scoop up cereal and milk

  7. Lift spoon to mouth and empty contents of the spoon into mouth

  8. Eat spoonful of cereal and milk

  9. Place spoon in bowl and scoop up cereal and milk

  10. Lift spoon to mouth and empty contents of the spoon into mouth

  11. Eat spoonful of cereal and milk

  12. Place spoon in bowl and scoop up cereal and milk

  13. Lift spoon to mouth and empty contents of the spoon into mouth

  14. Eat spoonful of cereal and milk

\quad \quad........continue these steps until bowl is finished (could be a large number of steps)

\quad \quadEnd

This algorithm could be rewritten using a loop:

  1. Fill bowl with cereal

  2. Pour milk onto cereal

  3. While there is cereal in the bowl:

  4. \qquad \quadPlace spoon in bowl and scoop up cereal and milk

  5. \qquad \quadLift spoon to mouth and empty contents of the spoon into mouth

  6. \qquad \quadEat spoonful of cereal and milk

  7. End

When writing algorithms in pseudocode we can use a WHILE loop. A while loop states a condition at the start of the loop which determines how many times a loop is repeated in an algorithm.

Look at this algorithm written in pseudocode, and the explanation of each line of code

\text{START}\text{start}
\text{SET $a = 2$}\text{give the variable $a$, the value 2}
\text{SET sum $ = 0$}\text{give the variable sum, the value 0}
\text{WHILE $a < 5$}\text{while $a$ has a value less than 5 repeat the next two steps}
\qquad \text{sum$=$sum}+a\text{add $a$ to the value of sum}
\qquad a = a + 1\text{add 1 to the value of $a$}
\text{END WHILE}\text{stop the loop when $a$ is no longer less than 5}
\text{PRINT sum}\text{print the final value of sum}
\text{END}\text{end}

This loop contains two steps:

\text{sum = sum} + a\\a = a + 1

these will be repeated until a is no longer less than 5. The values of the variables a and sum for each loop are:

\text{sum}a
\text{initially}02
\text{after first loop}0+2=22+1=3
\text{after second loop}2+3=53+1=4
\text{after third loop}5+4=94+1=5

At the end of the third loop the value of a is 5 and so the condition for the loop to end has been reached. The final value of sum is 9. This is the value that will be printed (\text{OUTPUT}) from the algorithm.

Examples

Example 1

Look at the pseudocode below:

1
START
2
SET a = 2
3
SET sum = 0
4
WHILE a < 9
5
sum = sum + a
6
a = a + 1
7
END WHILE
8
PRINT sum
9
END
a

The initial value of a is .

Worked Solution
Create a strategy

Look for the value \text{SET} for a.

Apply the idea

From line number 2, we can see that the initial value \text{SET} for a is 2.

b

What is the \text{OUTPUT} of this code?

Worked Solution
Create a strategy

Make a table of values for the loop in steps 5 and 6.

Apply the idea

Making a table of values for each loop until reaching a=9 we have:

\text{sum}a
\text{initially}02
\text{after first loop}0+2=22+1=3
\text{after second loop}2+3=53+1=4
\text{after third loop}5+4=94+1=5
\text{after fourth loop}9+5=145+1=6
\text{after fifth loop}14+6=206+1=7
\text{after sixth loop}20+7=277+1=8
\text{after seventh loop}27+8=358+1=9

After the seventh loop a=9 of 9, so the loop stops. At this point the sum is 35.

Since the last action is to \text{PRINT sum}, the output of this code is 35.

Example 2

Consider the algorithm represented by the flow chart:

The image shows a flow chart with branches. Ask your teacher for more information.
C:\text{a $= $a} + 1
E:\text{SET a =} 1
A:\text{WHILE a} ≠ 5
F:\text{PRINT sum}
B:\text{sum = sum + a}
G:\text{END WHILE}
D:\text{SET sum} = 0

Rearrange the letters A,B,C,D,E,F, and G so the corresponding pseudocode correctly represents the flow chart's algorithm.

Worked Solution
Create a strategy

Use the given flow chart to find the matching code and letter.

Apply the idea

The first action is \text{ sum}=0, so the first action is D.

The second action is a=1, so the next action is E.

Then, we have a decision where if a=5 we are going to \text{Print sum} if not, we are going to do two operations. But since we started with a number less than 5 we are going to repeat the two operations until the value is a=5. This corresponds to the while loop in A.

Since the prior step has a loop, the next lines must be the operations within the loop, which are \text{sum}=\text{sum}+a and a=a+1 in the flow chart. These correspond to B and C.

After reaching a=5 we can \text{END WHILE}, which is G.

Finally, we can \text{PRINT sum}, which is F.

So the correct order is D,\,E,\,A,\,B,\,C,\,G,\,F:

1
D: SET sum = 0
2
E: SET a = 1
3
A: WHILE a not equal to 5
4
B: sum = sum + a
5
C: a = a + 1
6
G: END WHILE
7
F: PRINT sum
Idea summary

Loops (or iterations) are steps that are repeated a number of times in an algorithm.

A WHILE loop continues until the WHILE condition is no longer true.

What is Mathspace

About Mathspace