20 min read
Structured Programming
Every program can be built from three structures — sequence, selection and iteration — combined in a clear, top-down design.
Three Building Blocks
| The three control structures | ||
|---|---|---|
Sequence
Read this flowchart as text
- Terminator: Start (then → Step 1)
- Process: Step 1 (then → Step 2)
- Process: Step 2 (then → Step 3)
- Process: Step 3 (then → Stop)
- Terminator: Stop
Selection
Read this flowchart as text
- Terminator: Start (then → Condition?)
- Decision: Condition? (Yes → Do this; No → Do that)
- Process: Do that (then → Stop)
- Process: Do this (then → Stop)
- Terminator: Stop
Iteration
Read this flowchart as text
- Terminator: Start (then → Repeat?)
- Decision: Repeat? (Yes → Do the step; No → Stop)
- Process: Do the step (then → Repeat?)
- Terminator: Stop
All Three in One Program
Sequence, selection and iteration togetherPython
# Sequence: set uptotal = 0 # Iteration: repeat for 3 itemsfor item in range(3): price = 1800 total = total + price # Selection: decide on the discountif total > 5000: total = total * 0.9 print("Amount to pay:", total)Expected output
Amount to pay: 4860.0
Top-Down Design
Top-down design (stepwise refinement) starts with the big task and breaks it into smaller and smaller sub-tasks until each is simple enough to code:
- Run the shop till
- - Enter the items → for each item, get price and quantity
- - Calculate the total → add up, apply discount
- - Print the receipt → heading, items, total, thank-you line
Why Structured Programming?
- Programs are easier to read and understand.
- They are easier to test and debug — each block can be checked on its own.
- They are easier to change later.
- Teams can work on different parts at the same time.
The opposite — code that jumps around with GOTO statements — was nicknamed "spaghetti code" because it was so tangled.
Practice
Which three structures are used in structured programming?
More lessons in Introduction to Programming · Next: Modular Programming · Previous: Types of Errors
