Three ways to think about a problem

Before anyone writes code, they think about the problem. Exam papers name three techniques.

  • Abstraction: remove the details that do not matter, so you can focus on the ones that do. A map of a railway shows which stations are on each line and where lines meet. It leaves out the bends in the track, because a passenger does not need them.
  • Decomposition: break a big problem into smaller problems that are each easier to solve. A quiz app becomes: store the questions, ask them, check the answers, keep the score.
  • Algorithmic thinking: work out the exact steps, in order, that solve the problem. "Check the password is at least 8 characters, then check it has a digit, then..."

Abstraction leaves things out. Decomposition splits things up. Algorithmic thinking puts steps in order.

A structure diagram draws a decomposition: the whole problem in the top box, its subsections in a row below, and the smaller tasks of each subsection below that. Each box can be planned, written and tested on its own, often as its own subroutine.

Writing an algorithm down

An algorithm is a sequence of steps that solves a problem. You can write one in three ways: numbered steps in plain English, a flowchart, or code (pseudocode or a real programming language).

Flowcharts use five symbols. You need to recognise all of them.

Start or stopProcessDecisionInput or outputSubroutine
  • A decision has two arrows out of it, labelled Yes and No.
  • A process is an action or a calculation, such as "total = total + price".
  • A subroutine box runs another part of the program, then comes back.

Inputs, processes and outputs

Most programs take something in, do something with it and give something back.

  • Input: the data that goes in (typed, read from a sensor, loaded from a file).
  • Process: what the program does to it (calculations, decisions, sorting).
  • Output: what comes back (shown on screen, printed, sent, saved, a sound).

A program that asks for a room's length and width, multiplies them and shows the area: the inputs are the length and width, the process is the multiplication, the output is the area.

Follow the steps

Work through the algorithm one step at a time. Keep a note of each variable as it changes, and be careful at the "go back" step: check the condition with the current values.