Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Programming Paradigms  ›  Lesson

Structured Programming

Structured Programming 12 minAny program can be built from sequence, selection and iteration, with each block having one way in and one way out.

Structured Programming

Early programs jumped around with goto: "if the payment failed, go to line 340". Reading one meant tracing arrows back and forth across the page, and a bug could hide in any jump. Structured programming replaced the arrows with a few block shapes, and every language you will use today is built on them, Shelfwise's code included.

The rule

Sequence, choice, repetition — every block has one way in and one way out.

Structured programming says any algorithm can be built from three building blocks:

  • Sequence: steps run one after another.
  • Selection: choose between paths with if / else (or match).
  • Iteration: repeat with for or while.

Blocks nest inside each other, and functions package a block under a name so it can be called like a single step. What is left out on purpose is the unrestricted jump. Without it, each block is entered at the top and left at the bottom, so you can understand it on its own.

def remind_unpaid(orders, today):
    for order in orders:                      # iteration
        if order.unpaid_days(today) > 7:      # selection
            text = reminder_text(order)       # sequence...
            send_email(order.customer, text)  # ...continues

Early return, break and continue are tame jumps that most modern style guides allow, because they exit a block in a way the reader can see. What structured programming ruled out is jumping into the middle of some other block.

The idea comes from the late 1960s, when computer scientists showed the three shapes were enough and argued that goto made programs hard to reason about. It won so completely that most beginners never see a goto at all.

The shortcut

To design any procedure, say it in plain words, then map each phrase to a block: "for each / while" is iteration, "if / unless / otherwise" is selection, "then" is sequence.

Designing the nightly stock alertStep 1 of 4
  1. Say it in words

    "For each book, if stock is below five, then look up the supplier, then send a restock request."

The trap is deep nesting. Three shapes nested six levels deep is still structured, but not readable. Pull inner blocks out into named functions.

Drills

Answer fast — trust the rule.

quick drill

Which is NOT one of the three structured building blocks?

quick drill

for order in orders: if order.late: flag(order) uses which blocks?

quick drill

Why does "one way in, one way out" make code easier to read?

quick drill

"While the payment keeps failing and we have tried fewer than three times, wait, then retry." Which block does "while" signal?

Remember

  • Sequence, choice, repetition; each block has one way in and one way out.
  • Say it in words, then map: "for each / while" loops, "if" chooses, "then" sequences.
  • Trap: structured but six levels deep; pull inner blocks into named functions.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainStructure the reminder job

Describe, using only sequence, selection and iteration, a job that emails a reminder to every Shelfwise customer whose order has been unpaid for more than seven days. Name which building block each part uses and say why the result is easy to read.