Sequencing in algorithms
An algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. is a plan, a set of step-by-step instructions to solve a problem. There are three basic building blocks (constructs) to use when designing algorithms:
- sequencing
- selection
- iteration
These building blocks help to describe solutions in a form ready for programmingThe process of writing computer software..
What is sequencing?
Algorithms consist of instructions that are carried out (performed) one after another.
For example, a very simple algorithm for brushing teeth might consist of these steps:
- put toothpaste on toothbrush
- use toothbrush to clean teeth
- rinse toothbrush
Each step is an instruction to be performed. Sequencing is the order in which the steps are carried out.
Why is sequencing important?
It is crucial that the steps in an algorithm are performed in the right order - otherwise the algorithm will not work correctly. Suppose the steps for the teeth-cleaning algorithm were in this sequence:
- use toothbrush to clean teeth
- put toothpaste on toothbrush
- rinse toothbrush
A toothbrush would still be used to clean the teeth and toothpaste would still be put on the brush. But because steps 1 and 2 are in the wrong sequence the teeth wouldn鈥檛 get cleaned with the toothpaste, and the toothpaste would be wasted.
A human would realise they had forgotten to add toothpaste at the start of the process, but a computer would not know that anything was wrong.
A computer can only do what it is programmed to do. If the steps are programmed in the wrong sequence, the computer will perform the tasks in this sequence 鈥 even if this is incorrect.