OCR H446, section 2.2.1 (a)
Every algorithm, however large, is built from just three constructs: sequence, iteration and branching (also called selection).
Sequence
Statements are carried out one after another, in the order they are written.
name = input("Name: ")
greeting = "Hello " + name
print(greeting)
The order matters (2.1.3 (c)): greeting can't be built until name has a value.
Branching (selection)
A condition decides which statements run.
if mark >= 70:
grade = "A"
elif mark >= 50:
grade = "B"
else:
grade = "C"
OCR pseudocode:
if mark >= 70 then
grade = "A"
elseif mark >= 50 then
grade = "B"
else
grade = "C"
endif
A switch/case statement chooses between many fixed values of one variable:
switch day:
case "Sat":
print("Weekend")
case "Sun":
print("Weekend")
default:
print("Weekday")
endswitch
(Python uses match/case, or if and elif.)
Iteration
Iteration repeats a block of statements. There are two kinds.
Count-controlled: for
Used when you know how many times to repeat.
for i in range(1, 6): # 1, 2, 3, 4, 5
print(i * i)
OCR pseudocode (note that to includes the last value):
for i = 1 to 5
print(i * i)
next i
Condition-controlled: while and do ... until
Used when you don't know how many repetitions are needed.
| Loop | Condition tested | Body runs |
|---|---|---|
while |
Before each pass | Zero or more times |
do ... until |
After each pass | At least once |
# while: stops when the condition becomes False
total = 0
while total < 100:
total = total + int(input("Add: "))
// do ... until: stops when the condition becomes True
do
password = input("Password: ")
until password == "letmein"
Python has no do ... until. A while True: loop with a break does the same job.
Nesting
Constructs can be placed inside each other. For example, a loop inside a loop:
for row in range(1, 4):
line = ""
for col in range(1, 5):
line = line + str(row * col) + " "
print(line)
Output:
1 2 3 4
2 4 6 8
3 6 9 12
The inner loop runs completely for every pass of the outer loop, so the body runs times.
Choosing the right construct
| Situation | Use |
|---|---|
| Steps always in the same order | Sequence |
| A choice between actions | Branching |
| A fixed number of repeats | for |
| Repeat until something happens, might not run at all | while |
| Repeat until something happens, must run at least once | do ... until |
| Process every item of an array | for over the indexes, such as for i = 0 to 4 for an array of 5 items |
OCR's pseudocode has no "for each" loop, so to visit every item you count through the indexes. Python's for item in items: is fine in program code, and any clear pseudocode style is accepted in the exam.
Worked example
Task: write a program that asks for numbers until the user enters 0, then displays how many were positive and how many were negative.
- Sequence: set the counters to 0 first, then read, then display.
- Iteration: a
whileloop, because the number of inputs isn't known. - Branching: inside the loop, decide whether the number is positive or negative.
positive = 0
negative = 0
n = int(input("Number (0 to stop): "))
while n != 0:
if n > 0:
positive = positive + 1
else:
negative = negative + 1
n = int(input("Number (0 to stop): "))
print("Positive:", positive)
print("Negative:", negative)
All three constructs appear, and the first input is read before the loop, so that the condition has a value to test.
Exam tip: name the construct, and say why it is the right one, for example "a while loop, because the number of repetitions isn't known in advance". Be ready to say when do ... until differs from while (always runs at least once).
Check yourself
Name the three programming constructs
Sequence, iteration and branching (selection).
When would you use a for loop instead of a while loop?
When the number of repetitions is known in advance.
What is the difference between a while loop and a do ... until loop?
A while loop tests the condition before each pass, so it may never run. A do ... until loop tests after each pass, so it runs at least once.
How many times does the inner statement run: for i in range(5): for j in range(3): ...?
15 times (5 × 3).