Video lecture
Watch on YouTube(opens in a new tab)Sub topic 8
Drawing Algorithms as Flowcharts
This chapter assumes you have read the earlier chapters on sequence, selection and iteration, and on pseudocode. You should be able to read the pseudocode version of Euclid's GCD algorithm. Here we draw that same algorithm as a picture.
What a Flowchart Is
A flowchart is a diagram of an algorithm. Each step is a shape, and arrows show which step runs next. Pseudocode shows control flow through indentation and keywords such as while. A flowchart shows it as arrows, so every possible path through the algorithm can be seen at once.
Flowcharts use a small set of standard shapes:
| Shape | Meaning | Example |
|---|---|---|
| Rounded box (terminal) | Start or end of the algorithm | Start, Stop |
| Parallelogram | Input or output | Input a, b |
| Rectangle (process) | An action that changes state | r := a mod b |
| Diamond (decision) | A yes/no question with two exits | Is b = 0? |
| Arrow | The next step to run |
The Three Building Blocks as Shapes
Each control structure has a recognisable shape in a flowchart:
- Sequence is a chain of rectangles joined by arrows in a single line.
- Selection is a diamond whose two exits follow different paths and later meet again.
- Iteration is a diamond with one exit that leads back up to an earlier point. The backward arrow is what makes the loop.
Euclid's Algorithm as a Flowchart
Here is the while b != 0 loop from the pseudocode chapter, drawn as a flowchart.
Compare it with the pseudocode:
- The
while b != 0test becomes the diamond. In the diagram the question is "Is b = 0?", so the "No" branch is the loop body. Phrase decisions so the branch labels read naturally, and check which branch is which. - The two assignments in the loop body become a sequence of rectangles.
end whilehas no shape of its own. Its job is done by the arrow that goes from the last rectangle back to the diamond.return abecomes an output parallelogram followed by Stop.
Common Errors
Other mistakes to watch for:
- Unlabelled decision exits. Every diamond needs exactly two exits, labelled Yes and No (or True and False).
- Missing Stop. Every path must reach a terminal shape. If a path cannot reach one, the algorithm may not terminate, and the drawing gives you no warning.
- Combined updates. In Python,
a, b = b, a % bupdates both variables at once. A flowchart runs one step at a time, so you need the temporaryror the order of the updates will be wrong.
When to Use a Flowchart
Flowcharts suit short algorithms with complicated branching, and readers who do not program. They grow large quickly. Nested loops and many variables make them harder to follow than pseudocode. For anything longer than about a dozen steps, prefer pseudocode, or draw only the top-level control flow.
Exercise
Draw (or describe) a flowchart for linear search: given a list items and a target, output the index of the first match or -1. How many diamonds does it need, and which arrow forms the loop?
Show answer
It needs two diamonds. After Start, Input items and target, and i := 0, the first diamond asks "Is i < len(items)?". If No, output -1 and Stop. If Yes, a second diamond asks "Is items[i] = target?". If Yes, output i and Stop. If No, do i := i + 1 and follow an arrow back to the first diamond. That backward arrow forms the loop.
Exercise
In the Euclid flowchart, which pseudocode line has no shape of its own, and what does its job instead?
Show answer
The "end while" line has no shape. The arrow from the final assignment back to the "Is b = 0?" decision does its job.