What is an algorithm?
An algorithm is a set of ordered steps that solves a problem or completes a task. A cooking recipe is an algorithm: it lists the steps in the right order so anyone can follow them. In computer science we plan an algorithm before writing a program, so we know exactly what the program must do.
Two ways to plan
We often write an algorithm in two forms. Pseudocode uses short lines of plain English that look like code but are easy to read. A flowchart is a diagram that uses shapes and arrows: an oval for start and end, 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 (a choice using IF), and loop (repeating steps).
The three control structures
- Sequence — do the steps one after another, top to bottom.
- Selection — choose a path with a condition, using
IF ... THEN ... ELSE. - Loop — repeat steps until a condition is met.
Example
Pseudocode to check a mark:STARTINPUT markIF mark >= 50 THEN OUTPUT "Pass"ELSE OUTPUT "Fail"END
The IF line is selection; the steps run in sequence from top to bottom.