GGCSE Revision Hub

3.2.5 Boolean operations in a programming language

AQA Computer Science 8525Topic 3.2 — ProgrammingFoundation & Higher

This is the longest topic in the specification and the one that carries most of the Paper 1 marks. It covers the statements every program is built from, the operators that do the work, the structures that hold data, and the discipline that makes code safe to rely on. Two of the specification's supported languages are Python and C#, so the examples here use Python-style syntax with the AQA pseudo-code where the specification demands it. Learn this as something you write, not something you memorise.

What you need to know

The three operators

Be familiar with and be able to use NOT, AND and OR. These are the Boolean operators, and they combine or reverse conditions. Where the relational operators compare two values, the Boolean operators work on the results of those comparisons.

Key fact: NOT, AND and OR combine and reverse the results of comparisons.

NOT

NOT reverses the truth value of a single condition. NOT (score > 50) is true when the score is 50 or less. It is the operator behind an ELSE branch that runs when a condition fails, and behind 'unless' in a set of instructions. It only ever applies to one thing, so NOT A AND B is ambiguous unless you use brackets.

Key fact: NOT reverses a condition; it applies to one condition at a time.

AND

AND is true only when both conditions are true. It is how you express a rule with more than one condition: IF score > 50 AND age > 16 THEN. It is also how you test a range properly, because AND has a lower precedence than a relational operator, so `a < x AND x < b` groups correctly.

Key fact: AND is true only when both conditions are true.

OR

OR is true when at least one of the conditions is true. It expresses a rule where any of several things is enough: IF temperature > 30 OR humidity > 80 THEN. It is the operator to reach for when a rule has alternative triggers, and it is also the one most often mis-written, because `a OR b AND c` is not the same as `(a OR b) AND c`.

Key fact: OR is true when at least one condition is true.

Brackets change everything

NOT, AND and OR do not all have the same precedence: NOT binds most tightly, then AND, then OR. So `NOT A AND B` means `(NOT A) AND B`, and `A OR B AND C` means `A OR (B AND C)`. Whenever the meaning is not obvious, add brackets — and in an exam answer, showing the brackets is showing that you understand it.

Key fact: NOT binds tighter than AND, and AND tighter than OR — use brackets when in doubt.

Using them in conditions

Students should be able to use these operators, and combinations of these operators, within conditions for iterative and selection structures. So every complex condition in a program is built out of comparisons combined with these three. When you are tracing a program, write the condition out in words and then translate it into a Boolean expression — that is the safest way to work out what a complex IF really does.

Key fact: Every complex condition is built from comparisons combined with NOT, AND and OR.

Flashcards

Exam questions

Written in the style of AQA Computer Science, with a mark scheme for each one. No question here is a real past paper question.

mcq1 mark

A condition is `NOT (age > 18) AND hasPassport`. When is it true?

  • When age is 18 or under and has a passport
  • When age is over 18 and has a passport
  • When age is over 18 or has a passport
  • When age is 18 or under or has a passport

Mark scheme: 1 mark for identifying that NOT reverses the age test so the age must be 18 or under, and AND requires the passport as well.

short3 marks

Explain in English the difference between `A OR B AND C` and `(A OR B) AND C`, and why this matters.

Mark scheme: 1 mark for stating that AND binds more tightly than OR, so A OR B AND C means A OR (B AND C). 1 mark for stating that with brackets it means (A OR B) AND C, which is a different condition. 1 mark for explaining that the two are true in different situations, so the brackets change what the program does and must be used whenever the meaning is not obvious.

six6 marks

Explain in English the three Boolean operators, how they combine with comparisons, and how to avoid misreading a complex condition.

Mark scheme: Level 1 (1-2 marks): A short answer naming the three operators and what they do. Level 2 (3-4 marks): A structured answer covering each operator and the precedence rules. Level 3 (5-6 marks): A detailed answer explaining that relational operators produce Booleans and that NOT, AND and OR combine them, describing the truth conditions of each, setting out the precedence order NOT then AND then OR, showing how brackets change the meaning with an example, and explaining the safe technique of rewriting a complex condition in words before translating it into a Boolean expression when tracing a program.

Other sections in this topic

Practise these questions

Answer them in the interactive view and they get marked against the same mark scheme, one mark at a time, with the reason for each mark.

Open the interactive topic →