OCR H446, section 2.1.4 (c)
Without decisions, a program runs in sequence, from the first line to the last. Decisions change the flow of control: they decide which lines run, how many times, and in what order.
Ways decisions change the flow
| Construct | Effect on the flow |
|---|---|
Selection (if, elif, else, match) |
Some statements are run, others skipped |
Count-controlled loop (for) |
Statements are repeated a set number of times |
Condition-controlled loop (while, do ... until) |
Statements repeat until a condition changes |
| Subroutine call | Flow jumps to the subroutine, then returns |
Early exit (break, return) |
The flow leaves a loop or subroutine immediately |
Exception handling (try/except) |
The flow jumps to the handler if an error occurs |
Selection: skipping and choosing
x = 7
if x > 5:
print("big") # runs
else:
print("small") # skipped
print("done") # always runs
Only one branch of an if/else runs. The flow joins up again afterwards.
Nested selection
A decision inside another is only reached if the first one's condition was met:
if age >= 18:
if has_ticket:
print("Enter")
else:
print("Buy a ticket")
else:
print("Too young")
Iteration: repeating
In a while loop, the condition is tested before each pass. If it's False at the start, the body never runs. If it never becomes False, the loop is infinite.
n = 10
while n > 0:
n = n - 3
print(n) # 10 -> 7 -> 4 -> 1 -> -2
A do ... until loop (OCR) tests the condition after the body, so the body runs at least once:
do
password = input("Password: ")
until password == "secret"
Decisions inside loops
A decision in a loop is tested every time round:
count = 0
for n in numbers:
if n % 2 == 0:
count = count + 1
Leaving a loop early
for n in numbers:
if n < 0:
print("Negative found")
break # the loop ends immediately
Following the flow: trace tables
A trace table records the value of each variable after each step, which shows exactly which path was taken.
Example:
total = 0
for n in [4, 7, 2, 9]:
if n > 5:
total = total + n
print(total)
n |
n > 5? |
total |
|---|---|---|
| (start) | 0 | |
| 4 | No | 0 |
| 7 | Yes | 7 |
| 2 | No | 7 |
| 9 | Yes | 16 |
The output is 16. The decision skipped the additions for 4 and 2.
Things that go wrong
- Wrong condition: the wrong path is chosen (boundary errors,
and/ormixed up). - Infinite loop: the condition is never made False, because the variable in the condition isn't changed in the loop.
- Off-by-one: the loop runs one time too many or too few.
- Unreachable code: a branch that can never be chosen because an earlier condition always catches it.
- Missing branch: no code for one of the possible outcomes.
Worked example
Task: trace the following algorithm for the input 6 and describe the flow.
n = 6
steps = 0
while n != 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
steps = steps + 1
print(steps)
The trace is:
| Loop | n at start |
n % 2 == 0? |
n after |
steps |
|---|---|---|---|---|
| 1 | 6 | Yes | 3 | 1 |
| 2 | 3 | No | 10 | 2 |
| 3 | 10 | Yes | 5 | 3 |
| 4 | 5 | No | 16 | 4 |
| 5 | 16 | Yes | 8 | 5 |
| 6 | 8 | Yes | 4 | 6 |
| 7 | 4 | Yes | 2 | 7 |
| 8 | 2 | Yes | 1 | 8 |
The loop test n != 1 is False after loop 8, so the loop ends and 8 is printed. The path depends on the data: the decision inside the loop picks a different branch depending on whether n is odd or even. The number of repetitions isn't known in advance, so a condition-controlled loop is the right choice.
Exam tip: when tracing, work through one step at a time, and record the result of each condition (True or False) as well as the variables. This shows which branch was taken. When describing flow, mention which statements are skipped or repeated and why.
Check yourself
How does selection change the flow of a program?
Some statements are run and others skipped, depending on whether a condition is true.
What is the difference between a while loop and a do ... until loop?
A while loop tests its condition before each pass, so the body might never run. A do ... until loop tests after the body, so it always runs at least once.
What causes an infinite loop?
The loop's condition never becomes false, often because nothing in the body changes the variables that the condition uses.
What is a trace table used for?
To follow the values of variables step by step, showing which path the program takes and what output it gives.