Sequencing in practice: Drawing a square
An algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. to get a computer to draw a square on the screen might consist of these steps:
- draw a 3 cm line
- turn left 90 degrees
- draw a 3 cm line
- turn left 90 degrees
- draw a 3 cm line
- turn left 90 degrees
- draw a 3 cm line
The steps in this algorithm are in the correct sequenceIn computer programming, this is a set of instructions that follow on one from another.. This algorithm would result in a perfect square:
If there was a mistake when designing the algorithm, and the steps in this sequence were placed like this:
- draw a 3 cm line
- turn left 90 degrees
- draw a 3 cm line
- turn left 90 degrees
- draw a 3 cm line
- draw a 3 cm line
- turn left 90 degrees
This algorithm would create this shape, rather than a perfect square:
Because step 6 is in the wrong sequence (it should switch with step 7), this algorithm failed. However, fixing the algorithm - and the square - is easy, because there are only seven steps to look through for the error.
Complex algorithms may have hundreds, if not thousands, of steps. It is critical to make sure all steps in the algorithm are in the correct sequence before programming begins. Once programmed, trying to find an instruction in the wrong sequence can be extremely difficult.