Designing Algorithms (Pseudocode & Flowcharts)
Before a programmer writes any code, they design an algorithm. An algorithm is a precise set of step-by-step instructions for solving a problem or completing a task. Planning the algorithm first — on paper or on screen — means you can spot mistakes in the logic before you spend time typing real program code. This lesson looks at four tools for designing algorithms: working out the inputs and outputs, drawing structure diagrams, and writing the steps as pseudocode or as a flowchart.
Inputs, processes and outputs
A good first step is to ask three questions: what information does the program need, what must it do with that information, and what should it produce? These are the inputs, the processes and the outputs. Take a program that works out the area of a rectangle. The inputs are the length and the width. The process is multiplying the length by the width. The output is the area, shown on the screen. Splitting a problem this way makes the design far clearer before you start coding.
Every program can be described by its inputs (the data going in), its processes (the steps that transform the data) and its outputs (the results that come out). Working these out first is the foundation of a good algorithm.
Breaking a problem down
Large problems are much easier to solve when they are split into smaller parts. This is called decomposition. A structure diagram shows this breakdown as a hierarchy: the whole problem sits in a box at the top, and the sub-tasks branch out beneath it, with the smallest jobs at the bottom. Reading from top to bottom takes you from the general goal down to the specific tasks the program must carry out.
Imagine building a quiz game. At the top is the whole task, ‘Run quiz game’. This breaks into three sub-tasks: ‘Ask questions’, ‘Keep score’ and ‘Show result’. ‘Ask questions’ then breaks down further into ‘Display question’, ‘Get answer’ and ‘Check answer’. Each of these small boxes is now simple enough to turn straight into code.
Run quiz game
Ask questions
Display question
Get answer
Check answer
Keep score
Show resultWriting pseudocode
Pseudocode is a way of writing an algorithm using short, code-like English. It is not a real programming language, so it does not have to follow exact syntax rules — but it does use common keywords so the logic is clear and could be turned into real code in any language. Pseudocode is quick to write and easy to read, which makes it ideal for planning.
score = input("Enter your score")
score = int(score)
if score >= 70 then
print("Grade: Pass with merit")
elseif score >= 50 then
print("Grade: Pass")
else
print("Grade: Try again")
endifThis short algorithm shows the three building blocks that every algorithm is made from. Sequence means the steps run one after another, in order. Selection means the program chooses between different paths using a condition, such as the if … then … else above. Iteration means repeating a set of steps, using loops such as for or while.
Assignment gives a value to a variable, written as total = 0. Input and output are usually written as input and print. Selection uses if, then, elseif, else and endif. Iteration uses a count-controlled loop, for i = 1 to 10 … next i, or a condition-controlled loop, while … endwhile. A variable is a named store that holds a value which can change while the program runs.
Algorithms are built from just three constructs: sequence (steps in order), selection (choosing a path with a condition) and iteration (repeating steps in a loop). Every program, however complex, is made from these three.
Drawing flowcharts
A flowchart is a diagram that shows an algorithm as a series of boxes joined by arrows. The shape of each box tells you what kind of step it is, and the arrows show the order the steps run in. Flowcharts are especially good at showing decisions and loops clearly.
Each flowchart is built from a standard set of symbols:
- Terminal — a rounded box that marks the start or the stop of the algorithm.
- Process — a rectangle for an action or a calculation, such as adding one to a score.
- Input/output — a parallelogram (a slanted box) for data going in or results coming out.
- Decision — a diamond holding a yes/no or true/false question, with a branch leaving each answer.
- Sub program — a rectangle with double side lines that calls a separate, named routine.
- Flow line — an arrow that connects the symbols and shows the direction of flow.
The real power of a flowchart comes from the decision symbol. A loop is drawn by sending one branch of a decision back to an earlier point, so the steps repeat until the condition is met. Following the arrows from the start terminal to the stop terminal traces the whole algorithm.
A login check starts at a terminal marked ‘Start’. An input/output symbol asks the user to enter a password. A decision symbol then asks ‘Is the password correct?’. If the answer is yes, a process grants access and the flow reaches the stop terminal. If the answer is no, the flow line loops back to the input symbol so the user can try again.
Checking an algorithm with a trace table
Once an algorithm is designed, you can check it works using a trace table. This is a grid with a column for each variable and each output. You work through the algorithm by hand, one step at a time, writing down how the values change. Trace tables are the best way to find logic errors, especially inside loops, because they show exactly what the program would do.
Suppose a loop starts with total = 0 and adds the numbers 3, then 5, then 2. A trace table records the value of total after each pass through the loop: it starts at 0, becomes 3, then 8, then 10. If the final total is not what you expected, the table shows you the exact step where the logic went wrong.
| Step | number | total |
|---|---|---|
| Start | — | 0 |
| Pass 1 | 3 | 3 |
| Pass 2 | 5 | 8 |
| Pass 3 | 2 | 10 |
Pseudocode or flowchart?
Both tools describe the same algorithm, so which you choose is often up to you. Pseudocode is fast to write and translates easily into real code, so programmers often prefer it for longer designs. Flowcharts are more visual and make branching and loops easy to see at a glance, which helps when explaining an idea to someone else. A skilled designer can move comfortably between the two.