GoalSay in which order NOT, AND and OR are worked out and use brackets to control the order.
New words
precedence · the order in which operators are worked outbrackets · ( ) used to make part of a condition happen firsttruth value · the result of a condition, True or False
Explanation
When a condition has more than one Boolean operator, the order matters. NOT is worked out first, then AND, then OR. This is like multiplication before addition in maths. Anything in brackets is worked out before everything else. Brackets can change the result, so use them whenever the meaning might be unclear, even when they are not needed. To work out a long condition, first replace every small condition with True or False. Then work out NOT, then AND, then OR, writing the shorter condition each time. Do not skip steps, because that is where mistakes happen.
Order
Operator
Example
After this step
1
brackets
(True OR False) AND False
True AND False
2
NOT
NOT False AND True
True AND True
3
AND
True OR False AND False
True OR False
4
OR
True OR False
True
Worked examples
Pseudocode
1INPUT age
2INPUT card
3IF age < 12 OR age > 60 AND card = "yes" THEN
4 OUTPUT "Discount"5ELSE
6 OUTPUT "Full price"7ENDIF
A cinema gives a discount to children under 12 and to people over 60, but only when they show a card. Without brackets AND is worked out first. For age 8 and card no the steps are: True OR (False AND False), then True OR False, and the answer is True. The output is Discount, which is not what the cinema wanted.
Pseudocode
1INPUT age
2INPUT card
3IF (age < 12 OR age > 60) AND card = "yes" THEN
4 OUTPUT "Discount"5ELSE
6 OUTPUT "Full price"7ENDIF
Brackets fix it. Now (age < 12 OR age > 60) is worked out first. For age 8 and card no: (True OR False) AND False is True AND False, and the answer is False. The output is Full price. For age 8 and card yes it is Discount.
Step
What is written
start
a < b OR c > 5 AND a > 6
replace each condition
True OR False AND False
AND first
True OR False
OR last
True
A step-by-step table for a = 4, b = 9, c = 2 and the condition a < b OR c > 5 AND a > 6. Notice that AND is worked out before OR.
Class activity
Order of play (no computer). Write six combined conditions on cards, for example ‘True OR False AND False’ and ‘NOT False AND False’. Shuffle them. Pick a card, and write the condition again after every step: first brackets, then NOT, then AND, then OR. Compare your final answers with a partner. Then add brackets to two cards so that the answer changes, and write both answers side by side. If you have a computer or tablet, ask an adult to check a card with Python’s True, False, and, or and not.
Practice
1
Work out a < b OR c > 5 AND a > 6 when a = 4, b = 9 and c = 2 again, but now with brackets round the OR part: (a < b OR c > 5) AND a > 6. Is the answer the same as without brackets?
(True OR False) AND False is True AND False, which is False. Without brackets the answer was True, so the brackets changed the result.
2
A museum gives free entry to people who are under 7 or over 70, but only when they have a booking. The condition is written without brackets: age < 7 OR age > 70 AND booking = "yes". Add brackets so that it means what the museum wants. Test your answer with age 5 and booking "no".
(age < 7 OR age > 70) AND booking = "yes". With age 5 and booking "no" it is (True OR False) AND False, which is True AND False, so False. Without the brackets the same input gives True OR (False AND False), which is True, so the person would wrongly get in.
3
Follow the algorithm for the inputs 5, 10, 19 and 20. What is shown each time?
Pseudocode
1INPUT n
2IF NOT (n < 10) AND n < 20 THEN
3 OUTPUT "Inside"4ELSE
5 OUTPUT "Outside"6ENDIF
5: NOT (True) is False, False AND True is False, so Outside. 10: NOT (False) is True, True AND True is True, so Inside. 19: Inside. 20: True AND False is False, so Outside.
4
This line has a mistake. Explain it and write the correct condition for ‘n is more than 3 and less than 9’.
Pseudocode
1IF n > 3 AND < 9 THEN
2 OUTPUT "Between"3ENDIF
The right side of AND is not a complete condition: it has no variable. Correct: n > 3 AND n < 9.