Logical Operators
OR is a logical (Boolean) operator that joins two conditions into one. The joined condition is true when at least one of the conditions is true, and it is false only when every condition inside it is false.
A truth table lists every combination of inputs and the result OR produces:
| Condition A | Condition B | A OR B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Look at the first row. OR is inclusive: when both conditions are true, the result is still true. It does not mean "one or the other but not both". If a rule can be met in two ways and you meet both of them, you have certainly met the rule.
Use OR in an algorithm when a rule can be satisfied in more than one way. A cinema sells child tickets to anyone whose age < 12 OR who has a family card. A 9-year-old with no card qualifies because the first condition is true; a 14-year-old with a card qualifies because the second condition is true; a 9-year-old with a card also qualifies, because a true condition on either side is enough. Only someone aged 12 or over with no card fails, since both conditions are then false.
age = int(input("How old are you? "))
card = input("Do you have a family card? yes or no: ")
if age < 12 or card == "yes":
print("Child ticket price") # one true test is enough
else:
print("Full ticket price") # both tests were falseOutput when the user types 9, no:
How old are you? 9
Do you have a family card? yes or no: no
Child ticket price
The two tests are joined by or, so the program takes the reduced-price branch as soon as one test passes. Run through the four combinations of the two tests and you get exactly the pattern in the truth table.
Edexcel GCSE Computer Science · Logical Operators
Edexcel GCSE Computer Science · Logical Operators