AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Two tests, one decision

Week 4 · 1.2 Selection in pseudocode · 3 lessons
2

Brackets and order

Computing 8: Topic 1.2, pp. 27–58
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.

OrderOperatorExampleAfter this step
1brackets(True OR False) AND FalseTrue AND False
2NOTNOT False AND TrueTrue AND True
3ANDTrue OR False AND FalseTrue OR False
4ORTrue OR FalseTrue
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.
StepWhat is written
starta < b OR c > 5 AND a > 6
replace each conditionTrue OR False AND False
AND firstTrue OR False
OR lastTrue
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?
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".
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
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