Foundations of Structured Programming
From the structured programming curriculum
Foundations of Structured Programming
TL;DR
Structured programming is a way of organizing your code to make it clear, maintainable, and easy to understand. It relies on a few basic control structures that dictate how your program flows. By sticking to these principles, you'll write much better code that's easier to debug and extend.
1. The Mental Model
Think of structured programming as building with LEGOs. Instead of throwing bricks together randomly, you use specific connectors and shapes to build sturdy, predictable structures. Your code becomes a similar well-organized structure.
2. The Core Material
In structured programming, all programs can be built using just three fundamental control structures: sequence, selection (or decision), and iteration (or repetition/looping). You connect these simple blocks to create any complex program logic you need.
Sequence
This is the simplest. Code executes one instruction after another, in the order they're written, from top to bottom. There are no jumps or skips.
# Example of sequence
print("First, this line runs.")
x = 10
y = x + 5
print(f"Then, this line runs with y = {y}.")
Selection (Decision)

Photo by Godfrey Atima on Pexels
Selection allows your program to make choices based on conditions. The most common way to do this is with if-else statements. Your program evaluates a condition; if it's true, one block of code runs; otherwise, a different block (or no block) runs.
# Example of selection
temperature = 25
if temperature > 30:
print("It's a hot day!")
elif temperature < 10: # 'elif' is a shortcut for 'else if'
print("It's a cold day!")
else:
print("The weather is mild.")
Iteration (Looping)

Photo by Stanislav Kondratiev on Pexels
Iteration allows a block of code to be repeated multiple times. This is incredibly useful for processing lists of items or running code until a certain condition is met. The two main types are "for" loops (for a known number of repetitions or iterating over a collection) and "while" loops (for repeating as long as a condition is true).
# Example of iteration (for loop)
for i in range(3): # This loop will run 3 times
print(f"Loop count: {i + 1}")
# Example of iteration (while loop)
count = 0
while count < 2:
print(f"While loop count: {count + 1}")
count += 1 # Important: change the condition or you'll have an infinite loop!
These three structures can be nested inside each other to create complex logic, but you never use goto statements which would allow uncontrolled jumps in your code. Using goto is often called "spaghetti code" because the flow becomes tangled and hard to follow.
Here's how these structures combine:
graph TD
Start --> A["Instruction 1 (Sequence)"]
A --> B{"Condition? (Selection)"}
B -- True --> C["Do this (Sequence)"]
B -- False --> D["Do that (Sequence)"]
C --> E["Loop Start (Iteration)"]
D --> E
E --> F{"Loop Condition True? (Iteration)"}
F -- Yes --> G["Repeat this block (Sequence)"]
G --> E
F -- No --> H["Instruction after loop (Sequence)"]
H --> End
3. Worked Example
Let's combine these into a simple program that allows a user to guess a secret number.
import random
secret_number = random.randint(1, 10) # Generate a random number between 1 and 10
guessed_correctly = False # This variable helps control our loop
attempts = 0
print("I'm thinking of a number between 1 and 10. Can you guess it?")
# The entire guessing process is an iteration (while loop)
while not guessed_correctly:
attempts += 1
try:
user_guess_str = input(f"Attempt {attempts}: Enter your guess: ")
user_guess = int(user_guess_str) # Sequence: trying to convert input
except ValueError:
print("That's not a valid number. Please try again.")
continue # Skip the rest of this loop iteration and go to the next attempt
# Selection: comparing the guess with the secret number
if user_guess == secret_number:
print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts.")
guessed_correctly = True # This will make the while loop condition false, ending the loop
elif user_guess < secret_number:
print("Too low! Try again.")
else: # user_guess > secret_number
print("Too high! Try again.")
In this example, we have:
* Sequence: Taking input, converting to int, printing messages.
* Iteration: The while loop continues until guessed_correctly is True.
* Selection: The if-elif-else block checks the guess and provides feedback.
4. Key Takeaways
- Programs are built from just three basic control structures: sequence, selection, and iteration.
- Sequence means instructions run one after another, in order.
- Selection (like
if-else) lets your program make decisions based on conditions. - Iteration (like
fororwhileloops) allows code blocks to repeat. - Avoiding
gotostatements is crucial for writing clear, maintainable "structured" code. - Combining these three structures is enough to build any program logic.
Common Mistakes to Avoid:
- Overcomplicating simple logic with unnecessary if statements.
- Creating infinite while loops by forgetting to change the loop's condition.
- Using magical "jump to" statements (not common in modern languages, but important concept).
- Not indenting your code consistently, which makes flow hard to follow.
5. Now Try It
Write a short Python program that asks the user for their age. If they are 18 or older, print "You are an adult." If they are under 18, tell them how many years until they are an adult. Then, use a loop to print "Happy Birthday!" for each year they've been alive up to their current age (e.g., if they're 3, print "Happy Birthday!" three times).
What success looks like: Your program will correctly handle both adult and minor ages, use an if-else for the age check, and a for or while loop to print the birthday messages the correct number of times.
Frequently asked about Foundations of Structured Programming
Get the full structured programming curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account