CS9: Boolean Operations
Master NOT, AND, OR truth tables, combining Boolean operators in conditions, De Morgan's Laws, and operator precedence.
Master NOT, AND, OR truth tables, combining Boolean operators in conditions, De Morgan's Laws, and operator precedence.
The three Boolean operators are:
| A | NOT A |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
isRaining ← TRUE
IF NOT isRaining THEN
OUTPUT "Let's go outside!"
ELSE
OUTPUT "Stay inside"
ENDIF
// Output: "Stay inside" (because NOT TRUE = FALSE)
| A | B | A AND B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE |
| FALSE | TRUE | FALSE |
| FALSE | FALSE | FALSE |
IF age >= 18 AND hasID = TRUE THEN
OUTPUT "You can buy alcohol"
ELSE
OUTPUT "You cannot buy alcohol"
ENDIF
// Both conditions must be TRUE to allow the purchase
| A | B | A OR B |
|---|---|---|
| TRUE | TRUE | TRUE |
| TRUE | FALSE | TRUE |
| FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE |
IF day = "Saturday" OR day = "Sunday" THEN
OUTPUT "It's the weekend!"
ELSE
OUTPUT "It's a weekday"
ENDIF
// Only one condition needs to be TRUE
IF (age >= 18 AND hasID = TRUE) OR isMember = TRUE THEN
OUTPUT "Access granted"
ENDIF
Access is granted if: (over 18 AND has ID) OR is a member.
IF (score >= 70 OR coursework >= 80) AND attendance >= 90 THEN
OUTPUT "Pass with merit"
ENDIF
Pass with merit requires: (high score OR high coursework) AND good attendance.
Let A = FALSE, B = FALSE, C = TRUE
(A AND B) OR C = (FALSE AND FALSE) OR TRUE = FALSE OR TRUE = TRUE
A AND (B OR C) = FALSE AND (FALSE OR TRUE) = FALSE AND TRUE = FALSE
Same values, different result - brackets matter!
Original: NOT (age >= 18 AND hasID = TRUE)
This means: it is NOT the case that both age >= 18 AND has ID.
Applying De Morgan's: (age < 18) OR (hasID = FALSE)
This means: either too young OR no ID - same meaning, simpler expression!
Original: NOT (raining = TRUE OR snowing = TRUE)
This means: it is NOT the case that it is raining OR snowing.
Applying De Morgan's: (raining = FALSE) AND (snowing = FALSE)
This means: it is not raining AND it is not snowing - same meaning!
| Original Expression | De Morgan Equivalent | English Meaning |
|---|---|---|
| NOT (x > 0 AND x < 100) | x <= 0 OR x >= 100 | x is outside the range 0-100 |
| NOT (a = TRUE OR b = TRUE) | a = FALSE AND b = FALSE | Both a and b are false |
| NOT (age >= 18 AND hasPassport) | age < 18 OR NOT hasPassport | Either too young or no passport |
NOT TRUE OR TRUE AND FALSE
Step 1: NOT TRUE = FALSE
Step 2: TRUE AND FALSE = FALSE
Step 3: FALSE OR FALSE = FALSE
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Using AND when you mean OR | "age < 13 AND age > 19" is always FALSE | Use OR: "age < 13 OR age > 19" |
| Forgetting brackets with mixed operators | A AND B OR C is ambiguous | Write (A AND B) OR C explicitly |
| NOT (A AND B) = NOT A AND NOT B | This is WRONG - De Morgan says OR, not AND | NOT (A AND B) = NOT A OR NOT B |
| Double negation confusion | NOT NOT TRUE = TRUE, but it's confusing | Simplify: remove double NOTs |
Q1: Complete the truth table for A AND B where A = TRUE, B = FALSE.
Q2: Evaluate: NOT (TRUE OR FALSE)
Q3: Use De Morgan's Laws to simplify: NOT (x >= 0 AND x <= 100)
Q4: Write a condition that checks if a person is eligible for a student discount (age < 18 OR hasStudentCard = TRUE) AND isShopping = TRUE.
Q5: Evaluate step by step: NOT FALSE AND TRUE OR FALSE
IF (age < 18 OR hasStudentCard = TRUE) AND isShopping = TRUE THEN
OUTPUT "Student discount applied"
ENDIF
✗ Thinking AND and OR are interchangeable ✓ AND requires ALL conditions to be true; OR requires at least ONE condition to be true. They produce different results and are not interchangeable.
✗ Forgetting that NOT reverses a Boolean value ✓ NOT TRUE = FALSE and NOT FALSE = TRUE. NOT flips the result — it is used to negate conditions, e.g. WHILE NOT finished.
✗ Assuming AND is evaluated before OR without brackets ✓ AND has higher precedence than OR in most languages. Use brackets to make intent clear: (A OR B) AND C is different from A OR (B AND C).
✗ Confusing NOT with negative numbers or minus signs ✓ NOT is a logical operator that reverses True/False. It is not the same as the arithmetic minus sign. NOT(5 > 3) = FALSE, not -5.
A login system requires a username of at least 5 characters AND a password of at least 8 characters. The user is locked out after 3 failed attempts. Write pseudo-code using Boolean operators to implement this. [5 marks]
attempts ← 0 loggedin ← FALSE WHILE attempts < 3 AND loggedin = FALSE INPUT 'Username: ', username INPUT 'Password: ', password IF LENGTH(username) >= 5 AND LENGTH(password) >= 8 THEN OUTPUT 'Login successful' loggedin ← TRUE ELSE attempts ← attempts + 1 IF NOT loggedin AND attempts < 3 THEN OUTPUT 'Invalid credentials, try again' ENDIF ENDIF ENDWHILE IF NOT loggedin THEN OUTPUT 'Account locked: too many attempts' ENDIF
AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including Boolean operations: AND, OR and NOT for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application — 40%): Apply knowledge and understanding of computer science, including Boolean operations: AND, OR and NOT to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including Boolean operations: AND, OR and NOT, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.