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(ormatch). - Iteration: repeat with
fororwhile.
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) # ...continuesEarly 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.
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.
Which is NOT one of the three structured building blocks?
for order in orders: if order.late: flag(order) uses which blocks?
Why does "one way in, one way out" make code easier to read?
"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.
