OCR H446, section 2.1.4 (a)
Thinking logically means working out where a solution must make decisions, and what each decision depends on. A decision point is a place where the program chooses between two or more paths.
Spotting decision points
Look in the problem for words such as:
- "if", "when", "unless", "otherwise"
- "depending on", "either ... or", "only if"
- A threshold or limit ("at least 18", "more than 10")
- A category that changes what happens ("student", "adult", "senior")
- Validation ("must be between 1 and 40")
- Repeating until something is true (a decision in a loop)
Each of these means the solution must test something and choose.
Kinds of decision
| Decision | Choice | Example |
|---|---|---|
| Yes or no | Do something or not | If the password is correct, log in |
| Either or | One of two actions | If the number is even, halve it, otherwise triple it and add 1 |
| Many ways | One of several | Grade A, B, C or fail |
| Loop control | Carry on or stop | Repeat until the user enters "q" |
| Validation | Accept or reject | Age must be from 0 to 120 |
| Error handling | Normal path or recovery | If the file isn't found, show a message |
Showing decisions
In a flowchart
A diamond with two exits (yes/no, true/false):
In code
if mark >= 50:
print("Pass")
else:
print("Fail")
OCR pseudocode:
if mark >= 50 then
print("Pass")
else
print("Fail")
endif
For several paths, use elseif (elif in Python), or switch/case (match in Python).
Locating the decisions: an approach
- Go through the problem and list every rule that starts with a condition.
- For each, write: what is tested, what happens if true, what happens if false.
- Check each decision has every outcome covered, including the "otherwise".
- Put the decisions in a sensible order (2.1.3 (c)).
Worked example
Problem: A theme park charges £12 for a standard ticket. Children under 5 go in free. Children aged 5 to 15 pay half price. People aged 65 or over pay £8. Entry is refused if the park is full.
| # | Decision point | Tested | Outcomes |
|---|---|---|---|
| 1 | Is the park full? | visitors >= capacity |
Yes: refuse entry. No: carry on |
| 2 | Is the visitor under 5? | age < 5 |
Yes: price 0 |
| 3 | Is the visitor 5 to 15? | age <= 15 |
Yes: price 6 |
| 4 | Is the visitor 65 or over? | age >= 65 |
Yes: price 8 |
| 5 | Otherwise | none | Price 12 |
def ticket_price(age, visitors, capacity):
if visitors >= capacity:
return None # entry refused
if age < 5:
return 0
elif age <= 15:
return 6
elif age >= 65:
return 8
else:
return 12
Notice that decision 1 comes first, because no price matters if entry is refused. The age tests are checked in order, so each only needs to test one boundary.
Exam tip: a decision point tests a condition and chooses a path. To find them, look for conditions and thresholds in the scenario. In a longer answer, say what is tested and what happens for each outcome.
Check yourself
What is a decision point in a solution?
A place where the program tests a condition and chooses between two or more paths.
Give three words or phrases in a problem description that suggest a decision is needed
Any three of: if, when, unless, otherwise, depending on, either/or, only if, at least, more than, until.
How does a flowchart show a decision?
With a diamond shape that has two (or more) exits labelled with the possible outcomes, such as yes and no.
How is a loop connected to decisions?
A loop contains a decision that tests its condition each time round, to choose between going round again and stopping.