Video lecture
Watch on YouTube(opens in a new tab)Sub topic 6
The Three Building Blocks: Sequence, Selection, Iteration
This chapter assumes you can read simple Python and that you have seen the plain-language version of Euclid's algorithm from the previous chapter. You should also be comfortable with if statements and loops in at least one language.
Every step-by-step procedure can be built from just three ways of arranging steps. A result known as the structured program theorem (Böhm and Jacopini, 1966) shows that sequence, selection and iteration together can express any computable procedure. Once you can spot these three in a description, you can rewrite that description in any notation.
The Three Structures
| Structure | What it does | Typical words in plain language | Python form |
|---|---|---|---|
| Sequence | Runs steps one after another, in order | "then", "next", numbered steps | consecutive statements |
| Selection | Chooses between steps based on a condition | "if", "otherwise" | `if` / `elif` / `else` |
| Iteration | Repeats steps while a condition holds or for each item | "repeat", "go back to", "for each" | `while`, `for` |
Finding the Structures in Euclid's Algorithm
Here is Euclid's plain-language description again:
- If b is 0, the answer is a. Stop.
- Otherwise, compute r, the remainder of a divided by b.
- Replace a with b, and b with r.
- Go back to step 1.
Step 1 is a selection. Steps 2 and 3 form a sequence. Step 4 ("go back") creates iteration. The test in step 1 also acts as the loop's exit condition. In Python, that combination becomes a single while loop:
def gcd(a, b):
while b != 0: # iteration; the test is the selection from step 1
a, b = b, a % b # sequence: compute remainder, then shift values
return a
print(gcd(48, 18)) # 6The step "go back to step 1" is effectively a jump instruction. Structured descriptions replace jumps with a loop whose condition is stated up front. This makes the loop's entry and exit points obvious to the reader.
Nesting: Structures Inside Structures
Real algorithms nest the three structures. Linear search puts a selection inside an iteration:
def linear_search(items, target):
for i, x in enumerate(items): # iteration over each position
if x == target: # selection inside the loop
return i
return -1 # sequence: runs after the loop ends
print(linear_search([7, 3, 9, 3], 3)) # 1
print(linear_search([7, 3, 9, 3], 5)) # -1Each structure has one way in and one way out. That property lets you treat any nested block as a single step when you reason about the block around it.
Common Errors
Exercises
Exercise
In this description, label each step as sequence, selection or iteration: "1. Set total to 0. 2. For each number n in the list: if n is even, add n to total. 3. Return total."
Show answer
Steps 1, 2 and 3 run in order, so together they form a sequence. "For each number n" is an iteration. "If n is even" is a selection nested inside that iteration. Step 3 runs once, after the loop finishes.
Exercise
A student writes a = b then b = a % b on separate lines inside Euclid's loop. What does gcd(48, 18) do, and why?
Show answer
The first pass sets a to 18, then b to 18 % 18 = 0. The loop exits and returns 18, which is wrong (the answer is 6). The second line reads the updated a, so the order of the sequence changed the meaning. Store the remainder first (r = a % b; a = b; b = r) or use tuple assignment.