SDD  ·  Software Design & Development

Complex Conditions: AND, OR, NOT

Lesson SDD8 of 18 Approx 65 min Requires SDD7 (selection)
Learning intentions
  • Understand how AND, OR, and NOT extend simple conditions into complex ones
  • Use truth tables to evaluate Boolean expressions step by step
  • Write complex conditions in SQA reference language and in Python
  • Apply operator precedence rules when mixing AND, OR, and NOT in one expression
Success criteria
  • I can describe what each Boolean operator does and give an example
  • I can complete a truth table for an expression using AND, OR, and NOT
  • I can trace the result of a complex condition for given variable values
  • I can write an IF statement with a complex condition in both SQA reference language and Python
  • I can explain why brackets are needed when mixing AND and OR
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge from selection (SDD7).

1. What does a Boolean expression always evaluate to?

2. In Python, which symbol checks whether two values are equal?

3. In SQA reference language, what keyword follows the condition in an IF statement?

Key vocabulary

Boolean expression
An expression that evaluates to either True or False
AND
Returns True only when both conditions are True
OR
Returns True when at least one condition is True
NOT
Reverses a Boolean value: NOT True gives False, NOT False gives True
complex condition
A condition built from two or more Boolean expressions joined by AND, OR, or NOT
truth table
A table that lists every possible combination of inputs and the resulting output for a Boolean expression
operator precedence
The order in which operators are evaluated. NOT is evaluated first, then AND, then OR
negation
Flipping a Boolean value to its opposite using NOT

Complex Conditions: AND, OR, NOT

Why simple conditions aren't always enough

A simple condition compares two values: age >= 16, score > 50. Many real problems need more than one condition checked at the same time. Should a user get a discount if they are under 12 or over 65? Should a door unlock only if the PIN is correct and the user is not suspended? These situations require complex conditions — two or more Boolean expressions joined by the operators AND, OR, or NOT.

The AND operator

AND joins two conditions and returns True only when both conditions are True. If either one is False, the whole expression is False. Think of it as a strict gate: every condition must pass.

ABA AND B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Example: a ride requires the rider to be at least 10 years old AND at least 130 cm tall. A 12-year-old who is 125 cm tall fails because, although the age condition is True, the height condition is False. AND gives False overall.

The OR operator

OR returns True when at least one of the conditions is True. Only when every condition is False does OR give False. Think of it as a lenient gate: any condition passing is enough.

ABA OR B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Example: a cinema gives a discount to children under 12 OR senior citizens over 65. A 70-year-old qualifies because the second condition is True even though the first is False.

The NOT operator

NOT works on a single condition and reverses it. NOT True gives False; NOT False gives True. It is used when you want something to be absent or untrue rather than present or true.

ANOT A
TrueFalse
FalseTrue

Example: a system sends a reminder only if the task is NOT complete. If complete = False, then NOT complete is True and the reminder is sent.

AND A B A AND B T T True T F False F T False F F False OR A B A OR B T T True T F True F T True F F False NOT A NOT A T False F True

Writing complex conditions in SQA reference language and Python

The logic is identical in both languages; only the formatting differs. SQA reference language uses capital AND, OR, NOT as keywords. Python uses the same words but entirely lowercase.

# SQA reference language
IF age >= 12 AND age <= 17 THEN
    SEND "Teen price applies." TO DISPLAY
END IF

# Python
if age >= 12 and age <= 17:
    print("Teen price applies.")

Both examples check that age is between 12 and 17 inclusive. In Python, and must be lowercase — writing AND in Python causes a syntax error.

Operator precedence and brackets

When AND and OR appear together in one expression, NOT is evaluated first, then AND, then OR. This is the same principle as BODMAS in arithmetic. The safest approach — and the one expected in the exam — is to use brackets to make the intended order explicit.

# Potentially ambiguous — which do you evaluate first?
IF isStudent OR isStaff AND hasTicket THEN ...

# Clear — brackets show exactly what is grouped
IF isStudent OR (isStaff AND hasTicket) THEN ...

Without brackets, AND is evaluated before OR, so isStaff AND hasTicket is evaluated first and then OR-ed with isStudent. Adding brackets makes this visible and removes any doubt.

Worked examples

Example 1 — Age range check with AND

A programme gives a junior discount to anyone aged between 5 and 15 inclusive. Evaluate whether a 10-year-old qualifies.

DECLARE age AS INTEGER INITIALLY 0
RECEIVE age FROM (INTEGER) KEYBOARD
IF age >= 5 AND age <= 15 THEN
    SEND "Junior discount applied." TO DISPLAY
ELSE
    SEND "No junior discount." TO DISPLAY
END IF
1
Evaluate the first condition: 10 >= 5True
2
Evaluate the second condition: 10 <= 15True
3
True AND True → True. The THEN branch runs: "Junior discount applied."
Example 2 — Eligibility check with OR

A leisure centre offers free entry to children (under 5) or senior citizens (over 70). Write the Python code and evaluate for a 3-year-old and a 45-year-old.

age = int(input("Enter age: "))
if age < 5 or age > 70:
    print("Free entry.")
else:
    print("Standard charge.")
1
For age = 3: 3 < 5 is True. True OR anything → True. Output: "Free entry."
2
For age = 45: 45 < 5 is False. 45 > 70 is also False. False OR False → False. Output: "Standard charge."
3
The OR means only one condition needs to be True. The 3-year-old qualifies; the 45-year-old does not.
Example 3 — NOT to reverse a condition

A system should display a reminder only when a task is not yet complete. The variable complete stores True or False.

# SQA reference language
IF NOT complete THEN
    SEND "Reminder: task still outstanding." TO DISPLAY
END IF

# Python
if not complete:
    print("Reminder: task still outstanding.")
1
If complete = False: NOT False → True. The reminder is displayed.
2
If complete = True: NOT True → False. The condition fails and no reminder appears.
3
NOT is cleaner and more readable than writing complete == False, though both are valid.
Example 4 — Combining AND, OR, and brackets

A streaming service allows access if the user has an active subscription OR (is a new user AND has entered a valid trial code).

hasSubscription = True
isNewUser = False
validCode = True

if hasSubscription or (isNewUser and validCode):
    print("Access granted.")
else:
    print("Access denied.")
1
Evaluate inside the brackets first: isNewUser AND validCode → False AND True → False
2
Evaluate the OR: hasSubscription OR False → True OR False → True
3
The condition is True so the output is "Access granted." — the subscription alone was enough.
Now you try

A theme park ride has the following rules: riders must be aged 10 or over AND at least 130 cm tall. If either requirement is not met, the rider is turned away.

Answer the following:

  1. Write the complex condition in SQA reference language using AND inside an IF statement that displays "You may ride." if the rider qualifies.
  2. Evaluate the condition manually for a rider who is 9 years old and 145 cm tall. Show each step.
  3. Write the Python version of the same condition.
  1. IF age >= 10 AND height >= 130 THEN
        SEND "You may ride." TO DISPLAY
    ELSE
        SEND "Sorry, you do not meet the requirements." TO DISPLAY
    END IF
  2. Step 1: 9 >= 10 → False. Step 2: 145 >= 130 → True. Step 3: False AND True → False. The rider is turned away — even though the height is fine, the age requirement fails AND requires both.
  3. if age >= 10 and height >= 130:
        print("You may ride.")
    else:
        print("Sorry, you do not meet the requirements.")
Common mistakes
Using && or || instead of AND / OR. Some other languages use && and ||, but SQA reference language uses the words AND and OR. Using symbols in an exam answer will not gain marks.
Writing AND, OR, NOT in capital letters in Python. Python is case-sensitive. AND in Python causes a syntax error — the correct Python keywords are and, or, not (all lowercase).
Confusing AND and OR. AND requires every condition to be True. OR only needs one to be True. A very common error is writing OR when the intended logic needs AND, or vice versa.
Omitting brackets when mixing AND and OR. A OR B AND C evaluates AND before OR because of precedence. If that is not what you intend, you must use brackets: A OR (B AND C) or (A OR B) AND C.
Writing a repeated variable incorrectly. A common slip is writing age == 16 OR 17. Python does not read this as two age comparisons — it reads 17 as a separate non-zero value, which is always truthy. The correct form is age == 16 or age == 17.
Exam tip

When asked to evaluate a complex condition, always work through it in steps. Write out each simple condition as True or False first, then apply the operator. For example, if the question gives x = 4 and the condition is x > 2 AND x < 10, write: True AND True → True. Showing your working earns method marks even if you make a minor arithmetic slip. Also note: the exam uses capital AND, OR, NOT — always match the style of the question you are answering.

Task Set

Questions 1–5 are auto-checked. Questions 6–9 are self-marked — write your answer, then reveal the model answer to check your work.

1. What does score >= 50 AND age >= 16 evaluate to when score = 60 and age = 15? TYPE 1

2. Which expression evaluates to True when hasMembership = False? TYPE 1

3. In Python, which of these correctly checks that x is between 1 and 10 inclusive? TYPE 1

4. The condition is x > 10 OR x < 0. Which value of x makes this False? TYPE 1

5. What is the result of NOT (True AND False)? TYPE 1

6. Write SQA reference language for a door security system. A door should unlock only if the PIN is correct AND the user is not suspended. Use variables pinCorrect (Boolean) and suspended (Boolean). Display "Door unlocked." or "Access denied." as appropriate. TYPE 2

IF pinCorrect AND NOT suspended THEN
    SEND "Door unlocked." TO DISPLAY
ELSE
    SEND "Access denied." TO DISPLAY
END IF

The condition requires both parts to be True: the PIN must be correct AND the user must not be suspended. NOT suspended is True when suspended is False.

7. Complete the truth table below for the expression A OR (NOT B). Fill in the missing values for NOT B and the final result. TYPE 2

ABNOT BA OR (NOT B)
TrueTrue??
TrueFalse??
FalseTrue??
FalseFalse??
ABNOT BA OR (NOT B)
TrueTrueFalseTrue
TrueFalseTrueTrue
FalseTrueFalseFalse
FalseFalseTrueTrue

The only False result is when A is False and B is True, because NOT True = False and False OR False = False.

8. Explain the difference between AND and OR. For each operator, give a realistic example of a situation where you would use it in a program. TYPE 2

AND returns True only when every condition is True. Use it when all requirements must be met at the same time. Example: a bank login requires the username to be correct AND the password to be correct — both must match, not just one.

OR returns True when at least one condition is True. Use it when any one of several alternatives qualifies. Example: a sports club gives a discount to members who are under 16 OR over 60 — satisfying either age group is enough to qualify.

9. Write a Python program that accepts a password from the user. The password is valid only if it is longer than 8 characters AND starts with the letter "P". Display "Password accepted." or "Invalid password." as appropriate. Hint: use len() to get the length and [0] to check the first character. TYPE 2

password = input("Enter password: ")
if len(password) > 8 and password[0] == "P":
    print("Password accepted.")
else:
    print("Invalid password.")

len(password) > 8 checks the length. password[0] == "P" checks the first character (index 0). Both must be True for the password to be accepted — hence AND.

Teacher notes — Shift+T to hide

Suggested timing: 65 minutes. Warm up 5 min; notes and truth table walkthrough 20 min; worked examples 10 min; now you try 5 min; task set 25 min.

Key misconception to address: AND does not mean "add both things together" — it means both must be True simultaneously. Many pupils assume OR is more restrictive than AND; in fact OR is more permissive.

Live demo suggestion: Start with two simple conditions on the board (e.g., age and membership). Ask pupils to decide AND or OR for different scenarios before any code is shown. This builds intuition before syntax.

Common pitfall to watch: In Python, x == 16 or 17 does not behave as expected — 17 is always truthy. Demonstrate this live so pupils see the bug before writing it in their own code.

Extension question: Ask all pupils to trace what happens when you have three conditions joined by AND and OR, and to draw a truth table with all 8 rows (2³ combinations).

SQA command words covered: describe, explain, evaluate, write, complete (truth table).