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:
Fill bowl with cereal
Pour milk onto cereal
Place spoon in bowl and scoop up cereal and milk
Lift spoon to mouth and empty contents of the spoon into mouth
Eat spoonful of cereal and milk
Place spoon in bowl and scoop up cereal and milk
Lift spoon to mouth and empty contents of the spoon into mouth
Eat spoonful of cereal and milk
Place spoon in bowl and scoop up cereal and milk
Lift spoon to mouth and empty contents of the spoon into mouth
Eat spoonful of cereal and milk
Place spoon in bowl and scoop up cereal and milk
Lift spoon to mouth and empty contents of the spoon into mouth
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:
Fill bowl with cereal
Pour milk onto cereal
While there is cereal in the bowl:
\qquad \quadPlace spoon in bowl and scoop up cereal and milk
\qquad \quadLift spoon to mouth and empty contents of the spoon into mouth
\qquad \quadEat spoonful of cereal and milk
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} | 0 | 2 |
\text{after first loop} | 0+2=2 | 2+1=3 |
\text{after second loop} | 2+3=5 | 3+1=4 |
\text{after third loop} | 5+4=9 | 4+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.
Look at the pseudocode below:
The initial value of a is ⬚.
What is the \text{OUTPUT} of this code?
Consider the algorithm represented by the flow chart:
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 |
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.