Combining AND, OR and NOT with brackets

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Combining AND, OR and NOT with brackets

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Without brackets, in what order is NOT A OR B AND C worked out?
A NOT first, then AND, then OR
B OR first, then AND, then NOT
2. Which condition means the same as A OR B AND C?
A (A OR B) AND C
B A OR (B AND C)
3. A = TRUE, B = FALSE, C = FALSE. What is (A OR B) AND C?
A TRUE
B FALSE
4. A = TRUE, B = FALSE, C = FALSE. What is A OR B AND C with no brackets?
A TRUE
B FALSE
5. A = TRUE, B = FALSE. What is NOT (A AND B)?
A TRUE
B FALSE

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1A OR B AND C ANOT A AND B
2(A OR B) AND C BMeans A OR (B AND C), because AND is worked out before OR when there are no brackets
3Brackets make NOT apply to a whole condition, so it is true only when both A and B are false CA OR B is worked out first, then the result must be true together with C
4Without brackets, NOT is done first and only reverses the value right after it DThe condition was worked out left to right. AND comes first: FALSE AND FALSE is FALSE, then TRUE OR FALSE is TRUE.
5IF Age < 12 OR Age > 65 AND HasTicket ENOT (A OR B)
6TRUE OR FALSE AND FALSE = FALSE FAND is done before OR, so HasTicket only applies to Age > 65. Brackets are needed: (Age < 12 OR Age > 65) AND HasTicket.

Definitions

(A OR B) AND C

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
IF (Age < 12 __ Age > 65) AND Paid THEN OUTPUT "Half price" ENDIF
2.
IF ___ (Raining OR Windy) THEN OUTPUT "Play outside" ENDIF
3.
Result <- (TRUE OR FALSE) ___ FALSE // Result is FALSE
4.
Result <- TRUE __ FALSE AND FALSE // AND first, so Result is TRUE
5.
Result <- NOT (TRUE AND _____) // Result is TRUE

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A IF (A OR B AND C THEN
B OUTPUT "Go"
C ENDIF
Question 2
A INPUT Age
B IF Age < 5 AND Age > 64 THEN
C OUTPUT "Child or senior"
D ENDIF
Question 3
A Rain <- FALSE
B Wind <- TRUE
C IF NOT Rain OR Wind THEN
D OUTPUT "Play outside"
E ENDIF
Question 4
A A <- TRUE
B R <- (A OR FALSE) AND FALSE
C OUTPUT R
D // Output is TRUE
Question 5
A INPUT Age
B INPUT Late
C IF Age < 18 OR NOT Late THEN
D OUTPUT "Young and on time"
E ENDIF

Answer Key

Combining AND, OR and NOT with brackets
Section A: Quick Questions
Q1
A — NOT first, then AND, then OR
Q2
B — A OR (B AND C)
Q3
B — FALSE
Q4
A — TRUE
Q5
A — TRUE
Section B: Match the Pairs
Q1
A OR B AND C → Means A OR (B AND C), because AND is worked out before OR when there are no brackets
Q2
(A OR B) AND C → A OR B is worked out first, then the result must be true together with C
Q3
Brackets make NOT apply to a whole condition, so it is true only when both A and B are false → NOT (A OR B)
Q4
Without brackets, NOT is done first and only reverses the value right after it → NOT A AND B
Q5
IF Age < 12 OR Age > 65 AND HasTicket → AND is done before OR, so HasTicket only applies to Age > 65. Brackets are needed: (Age < 12 OR Age > 65) AND HasTicket.
Q6
TRUE OR FALSE AND FALSE = FALSE → The condition was worked out left to right. AND comes first: FALSE AND FALSE is FALSE, then TRUE OR FALSE is TRUE.
Section C: Definitions
Q1
(A OR B) AND C — Brackets are worked out first. Without them NOT is done first, then AND, then OR. So A OR B AND C means A OR (B AND C), and brackets change that order.
Section D: Fill in the Blanks
Q1
IF (Age < 12 OR Age > 65) AND Paid THEN OUTPUT "Half price" ENDIF
Q2
IF NOT (Raining OR Windy) THEN OUTPUT "Play outside" ENDIF
Q3
Result <- (TRUE OR FALSE) AND FALSE // Result is FALSE
Q4
Result <- TRUE OR FALSE AND FALSE // AND first, so Result is TRUE
Q5
Result <- NOT (TRUE AND FALSE) // Result is TRUE
Section E: Spot the Error
Q1
A — The opening bracket has no closing bracket. Brackets must come in pairs. It should be IF (A OR B) AND C THEN so that A OR B is worked out first.
Q2
B — No age can be both under 5 and over 64, so AND is never true and nothing would be output. To match a child or a senior it must be OR: IF Age < 5 OR Age > 64 THEN.
Q3
C — Without brackets NOT is done first and only applies to Rain, so it is (NOT Rain) OR Wind. That is TRUE when it is windy. The brackets are needed so NOT applies to the whole of Rain OR Wind: IF NOT (Rain OR Wind) THEN.
Q4
B — These brackets make OR happen first: TRUE AND FALSE gives FALSE, not the TRUE the comment expects. AND is worked out before OR, so the brackets belong round FALSE AND FALSE: R <- A OR (FALSE AND FALSE).
Q5
C — The message says the person is young AND on time, so both parts must be true. OR would let in anyone under 18 even if they are late. It should be IF Age < 18 AND NOT Late THEN.