What is an algorithm?
An algorithm is a finite, ordered list of clear steps that solves a problem or completes a task. A good algorithm has a definite start and end, uses steps that are precise and unambiguous, and always finishes after a limited number of steps. Before writing any program, a good programmer first works out the algorithm, because a clear plan makes the code easier to write, to explain and to check for mistakes.
Two ways to write one
We plan algorithms before coding using two common tools:
- Flowchart — a diagram of connected shapes. An oval marks the start or end, a rectangle shows a process, a parallelogram shows input or output, and a diamond shows a decision.
- Pseudocode — plain-language steps written a bit like code, but without strict grammar rules.
Key idea
Every algorithm is built from three control structures: sequence (steps in order), selection (choosing a path with a condition), and repetition (repeating steps in a loop).
The three structures
Sequence runs steps one after another. Selection uses a condition to pick between paths, often written with IF ... THEN ... ELSE. Repetition (a loop) repeats steps while a condition stays true. Most everyday tasks, such as making a drink or sorting a list of names, can be broken down into these three structures. By combining them, we can describe the solution to almost any problem in a way that a computer can later be programmed to follow.
Example
Pseudocode to check a pass mark:
INPUT mark
IF mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF