An algorithm is a clear, step-by-step set of instructions for solving a problem. Programmers describe algorithms in two common ways before coding: pseudocode and flowcharts.
Pseudocode and flowcharts
Pseudocode is a plain-language outline of the steps; it is not a real programming language, so it has no strict grammar. A flowchart shows the same steps as connected symbols. Common flowchart symbols include an oval for start/end, a parallelogram for input/output, a rectangle for a process, and a diamond for a decision.
Key idea
Every algorithm is built from three control structures: sequence (steps in order), selection (choose a path with a condition) and repetition (repeat steps in a loop).
The three structures
- Sequence — instructions run one after another, top to bottom.
- Selection — an
IFcondition decides which steps run. - Repetition — a loop repeats steps while a condition is true.
Example
Pseudocode to check if a number is positive uses selection:
INPUT number
IF number > 0 THEN
OUTPUT "Positive"
ELSE
OUTPUT "Not positive"
ENDIF
A good algorithm should also be clear, finite and correct: it must have a definite start and end, use unambiguous steps, and always give the right result for valid input. Writing the algorithm first lets you check this logic on paper before you spend time coding, and it makes the coding phase much faster because you already know exactly what each step must do. The same algorithm can then be turned into any programming language, which is why a clear algorithm is one of the most valuable skills a young programmer can build. You can even test an algorithm by hand, tracing each step with sample values to make sure it gives the correct answer.