TopMyGrade

GCSE/Computer Science/AQA

CS2.4Relational and Boolean operations: =, ≠, <, ≤, >, ≥; AND, OR, NOT; building compound conditions in selection and iteration

Notes

Relational and Boolean operations

Decisions in programs depend on Boolean expressions — expressions that evaluate to true or false. These are built from relational operators (compare two values) and logical operators (combine Boolean values).

Relational operators

OperatorMeaningExampleResult
=Equal to5 = 5true
!=Not equal5 != 6true
<Less than4 < 5true
<=Less than or equal5 <= 5true
>Greater than6 > 5true
>=Greater than or equal6 >= 5true

These work on integers, reals, characters and strings (alphabetical order). For strings, "apple" < "banana" is true (a comes before b).

Logical (Boolean) operators

OperatorMeaningTruth table
ANDBoth inputs trueT AND T = T; T AND F = F; F AND F = F
ORAt least one input trueT OR T = T; T OR F = T; F OR F = F
NOTInverts the inputNOT T = F; NOT F = T

These often appear in compound conditions:

IF age >= 18 AND hasLicense = true THEN
  OUTPUT "Can drive"
ENDIF

The whole expression is true only when both sub-expressions are true.

IF day = "Saturday" OR day = "Sunday" THEN
  OUTPUT "Weekend!"
ENDIF

The expression is true if either side is true.

IF NOT loggedIn THEN
  OUTPUT "Please sign in"
ENDIF

NOT inverts a Boolean.

Operator precedence in Booleans

The usual order is: NOT first, then AND, then OR. Use brackets to make intent clear:

IF (a > 0 AND b > 0) OR c = 0 THEN ...

Without brackets, AND binds tighter than OR. a OR b AND c means a OR (b AND c).

Common Boolean idioms

  • Range check: x >= 0 AND x <= 100
  • Outside range: x < 0 OR x > 100
  • Either-or, not both (XOR): (a OR b) AND NOT (a AND b) — XOR isn't built into AQA pseudocode but can be expressed.
  • Contains check (string): depends on language; often a function like POSITION(s, c) > 0.

De Morgan's laws (HT but useful)

  • NOT (A AND B) = (NOT A) OR (NOT B)
  • NOT (A OR B) = (NOT A) AND (NOT B)

These help simplify conditions and avoid double negatives. E.g. "neither raining nor sunny" = NOT (raining OR sunny) = NOT raining AND NOT sunny.

Worked exampleWorked examples

Example 1. Evaluate (5 > 3) AND (10 = 10).

  • 5 > 3 → true
  • 10 = 10 → true
  • true AND true → true

Example 2. Evaluate NOT (4 < 2 OR 6 > 1).

  • 4 < 2 → false
  • 6 > 1 → true
  • false OR true → true
  • NOT true → false

Example 3. Decide whether to give a discount: customer must be over 65 OR have a loyalty card.

  • IF age > 65 OR hasLoyaltyCard = true THEN apply discount

Common mistakesPitfalls

  1. Using AND instead of OR (or vice versa). "x is between 1 and 10" → AND. "x is 1 or x is 2" → OR.
  2. Comparing a string to an integer → always false in many languages.
  3. Overusing brackets to the point of confusion — group related conditions clearly.
  4. Forgetting that NOT has highest precedence: NOT a OR b means (NOT a) OR b.
  5. Confusing assignment (SET x TO 5) with comparison (x = 5).

Truth table practice

Evaluate P AND (Q OR NOT R) for P=T, Q=F, R=T:

  • NOT R → F
  • Q OR F → F
  • P AND F → F

Try thisQuick check

Build a Boolean condition for "user is allowed to enter": age must be at least 12, AND either they have a ticket OR they are with a paying adult.

age >= 12 AND (hasTicket = true OR withPayingAdult = true)

AI-generated · claude-opus-4-7 · v3-deep-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 14 marks

    Truth tables

    Complete the truth tables for AND and OR with two Boolean inputs P and Q.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 23 marks

    Range condition

    Write a Boolean expression that is true when an integer x is between 10 and 99 inclusive.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 34 marks

    Evaluate

    Evaluate (5 > 3) AND NOT (4 = 2 OR 1 > 1).

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 43 marks

    OR vs AND

    Explain the difference between using OR and AND in this condition: "the user is in year 11 OR/AND the user is over 16".

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 53 marks

    NOT operator

    Simplify NOT (NOT x) and explain.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 64 marks

    Compound condition

    Write a Boolean expression that is true when (age >= 18 AND has voter ID) OR (age >= 16 AND in Scotland).

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 73 marks

    De Morgan

    Rewrite NOT (a > 0 AND b > 0) without using NOT on the whole expression.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS2.4 — Relational and Boolean operations

11-card SR deck for AQA GCSE Computer Science topic CS2.4

11 cards · spaced repetition (SM-2)