Structured Programming

Structured programming builds an application from nested blocks of code called block statements. This one idea — that any program can be assembled from a small set of predictable block shapes — is what lets a program branch and repeat without collapsing into unreadable "spaghetti code," the natural failure mode of linear programming once you start adding jumps.

Programs run in sequence by default. Three block shapes, nested inside that sequence, cover almost everything else:

  1. Decision — a block that runs, or doesn't, depending on one condition.
  2. Repetition — a block that runs several times.
  3. 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

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

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

Switch Diagram

Not every language has a dedicated selection statement. Python had none until version 3.10 added 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:

  1. Start from an initial state;
  2. Ask for input;
  3. Change its internal state;
  4. Make decisions;
  5. Execute one sequence of code or another;
  6. Repeat a sequence of code one or many times;
  7. Reach one or more final states.
Structured programming was made famous by Algol, Pascal, Modula, and Ada, and Fortran adopted it in its 1977 standard. Its three block shapes remain the backbone of control flow in every modern multi-paradigm language — C, C++, Rust, Go, and Scala all still express "decision, repetition, selection" exactly as described above; see any language's own Control Flow lesson in this site's language roadmaps for the concrete syntax.

Representative Languages

These four languages show structured programming at different levels: teaching, systems, science and safety.

LanguageWhy study it
PascalBuilt to teach structure: declarations first, strong types, clear begin ... end blocks.
CA small structured core plus pointers and manual memory. The base of most systems software.
FortranThe oldest high-level language, now structured and array-oriented for scientific computing.
AdaStructure 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?