Programs and algorithms alike use control flow to make decisions about the order in which to do things. There are three ways of doing this: sequence, selection and iteration.
Any basic algorithm uses sequences abundantly. This runs commands one after another, step by step. For example, if I were to be making a cup of tea:
Boil kettle
Pour boiling water into mug
Add teabag
Add sugar
Add milk
Remove teabag
Drink tea
Of course, the person for whom we are making this cup of tea may not want sugar in their tea. That’s where we can use selection.
Selection allows us to make decisions in our code. The most common type is the IF… THEN… structure - also known as a conditional. In our tea example, this could look like this:
Boil kettle
Pour boiling water into mug
Add teabag
IF sugar is wanted THEN
Add sugar
Add milk
Remove teabag
Drink tea
Of course, we may have to make cups of tea for multiple people. We can use iteration for this purpose. This involves doing a certain thing or set of things until a certain condition is met. For example:
REPEAT
Boil kettle
Pour boiling water into mug
Add teabag
IF sugar wanted THEN
Add sugar
Add milk
Remove teabag
UNTIL no more tea is needed
The example above uses the AQA REPEAT… UNTIL… syntax, however the same could be yielded with a WHILE loop.
There are 5 main types of iterating loops, each one working slightly differently: