OCR H446, section 2.1.4 (b)
Once you've found a decision point (2.1.4 (a)), you need to work out exactly which conditions decide the outcome, and how they combine.
Simple conditions
A condition is an expression that is either True or False. It usually compares values:
| Operator | Meaning |
|---|---|
== |
equal to |
!= |
not equal to |
< <= |
less than, less than or equal to |
> >= |
greater than, greater than or equal to |
Combining conditions
| Operator | Result is True when | OCR notation |
|---|---|---|
| AND | both conditions are true | AND or ∧ |
| OR | at least one is true | OR or ∨ |
| NOT | the condition is false | NOT or ¬ |
Truth tables
| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| False | False | False | False | True |
| False | True | False | True | True |
| True | False | False | True | False |
| True | True | True | True | False |
Examples
# Both must be true
if age >= 18 and has_id:
print("Entry allowed")
# Either is enough
if is_member or has_voucher:
print("Discount")
# Reversing a condition
if not logged_in:
print("Please log in")
Getting the conditions right
1. Boundaries
Decide whether the limit is included. "At least 18" means >= 18. "More than 18" means > 18. A wrong boundary is the commonest logic error. Test the boundary value and the values either side of it: 17, 18, 19.
2. Ranges
A value between 1 and 10 inclusive needs both limits, joined with AND:
if 1 <= n <= 10: # same as n >= 1 and n <= 10
A value outside a range uses OR:
if n < 1 or n > 10:
print("Out of range")
3. De Morgan's laws
The opposite of a combined condition:
- NOT (A AND B) is the same as (NOT A) OR (NOT B)
- NOT (A OR B) is the same as (NOT A) AND (NOT B)
# these are equivalent
not (age >= 18 and has_id)
age < 18 or not has_id
4. Order of evaluation
NOT is worked out first, then AND, then OR. Use brackets to make the meaning clear:
if (a or b) and c: # not the same as a or (b and c)
5. Nested decisions
Sometimes a condition is only tested if an earlier one was true. Combining the conditions with AND gives the same result as nesting:
if age >= 18:
if has_id:
print("Allowed")
is the same as if age >= 18 and has_id:.
Worked example
Problem: A gym offers a discount to students who are members for more than a year, and to anyone aged 65 or over. A customer can't get the discount if their account is overdue.
Define the conditions:
S: is a studentM: membership longer than 1 yearO: is 65 or overD: the account is overdue
The discount applies when (S AND M) OR O, and not D:
if ((student and years > 1) or age >= 65) and not overdue:
print("Discount")
Test with some cases:
| Student | Years | Age | Overdue | Discount? | Reason |
|---|---|---|---|---|---|
| Yes | 2 | 20 | No | Yes | Student, member for more than a year |
| Yes | 1 | 20 | No | No | Exactly 1 year is not more than 1 |
| No | 0 | 70 | No | Yes | 65 or over |
| No | 3 | 70 | Yes | No | Overdue account |
| No | 5 | 30 | No | No | Not a student, under 65 |
The second row tests a boundary (years > 1 with a value of exactly 1), where mistakes are likely.
Exam tip: when you write a condition, check the boundary (> or >=) and whether the parts are joined with AND or OR. Use brackets when you mix them. Test it with values on and either side of each boundary.
Check yourself
Write a condition that is true if x is between 5 and 10 inclusive
x >= 5 and x <= 10 (or 5 <= x <= 10 in Python).
Write a condition that is true if x is outside the range 5 to 10 inclusive
x < 5 or x > 10.
When is A AND B true, and when is A OR B false?
A AND B is true only when both are true. A OR B is false only when both are false.
Give an equivalent condition to NOT (A AND B)
(NOT A) OR (NOT B), from De Morgan's law.
Which values would you test for a condition age >= 18?
The boundary and either side of it: 17, 18 and 19.