Structured Programming
Programs run in sequence by default. Three block shapes, nested inside that sequence, cover almost everything else:
- Decision — a block that runs, or doesn't, depending on one condition.
- Repetition — a block that runs several times.
- Selection — a block chosen from several, based on a value.
Decision
Usually written with if/then/else keywords (some languages use when instead). A decision has one condition and two blocks. The condition is normally a logical expression, though some languages also accept things like "zero or non-zero" or "null or not null" in its place.
Decision Diagram
Repetition
Structured programming has several flavors of repetition, but the idea is the same in each: start a block with a condition, then repeat the block until the condition becomes false.
Repetition Diagram
Selection
A selection statement has many blocks — labeled 1, 2, 3, 4, ... 0 in the diagram below. Depending on the language it is called switch, case, match, or check; whatever the name, it uses one value to pick a single path of execution.
Switch Diagram
match, and a chain of if/elif did the same job. That is a small proof that selection is a convenience built on decision, and that sequence, decision and repetition are the true core (the Böhm-Jacopini theorem).What Structured Programming Gives You
A language built entirely from sequence, decision and repetition is already Turing complete. Using nothing but these blocks, a structured program can:
- Start from an initial state;
- Ask for input;
- Change its internal state;
- Make decisions;
- Execute one sequence of code or another;
- Repeat a sequence of code one or many times;
- Reach one or more final states.
Representative Languages
These four languages show structured programming at different levels: teaching, systems, science and safety.
| Language | Why study it |
|---|---|
| Pascal | Built to teach structure: declarations first, strong types, clear begin ... end blocks. |
| C | A small structured core plus pointers and manual memory. The base of most systems software. |
| Fortran | The oldest high-level language, now structured and array-oriented for scientific computing. |
| Ada | Structure plus range types and built-in checks, for software that must not fail. |
Structured programming answers "how does control flow through one function?" The next paradigm, object-oriented programming, answers a different question: how do you organize data and the code that acts on it once a program grows past a handful of functions?