Operators and Expressions · Cambridge syllabus 8.1 · Lesson 3 of 4
Logical Operators
AND requires both conditions, OR requires at least one, and NOT reverses a Boolean value. Parentheses communicate how combined conditions are grouped. Evaluate intermediate results to avoid confusing logical and arithmetic operations.
By the end, you can
- Use AND.
- Use OR.
- Use NOT.
- Combine conditions to solve programming problems.
Logic in programs
The logical operators combine Boolean values. AND gives TRUE only when both sides are TRUE. OR gives TRUE when at least one side is TRUE. NOT reverses a single value. The syllabus uses only these three.
They are how a real condition is expressed, because real conditions rarely test one thing. IF Age >= 13 AND Age <= 19 tests a range. IF Day = "Saturday" OR Day = "Sunday" tests for either of two values. WHILE NOT Found DO keeps a search going.
Each side of AND or OR must be a complete comparison. IF Age >= 13 AND <= 19 is not valid, and IF Day = "Saturday" OR "Sunday" is worse, because in some languages it is accepted and is always true.
In a complex condition, use brackets to make the order explicit, as the syllabus advises: IF (Age >= 12 AND HasTicket = TRUE) OR IsMember = TRUE leaves no doubt that the AND is worked out first. Without the brackets, a reader would have to guess.
Answer an exam question on these by working out each comparison first, then combining them. With Age = 15 and HasTicket = FALSE and IsMember = TRUE: Age >= 12 is TRUE, HasTicket = TRUE is FALSE, so the bracket is FALSE; FALSE OR TRUE is TRUE, so the whole condition is TRUE.
Worth remembering
- AND needs both true, OR needs at least one, NOT reverses.
- Each side of AND and OR must be a complete comparison of its own.
- Bracket combined conditions so the order is explicit.
Worked example
Eligible ← Age >= 12 AND HasPermission is true only when both requirements are satisfied.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
TRUE AND FALSE is FALSE; NOT FALSE is TRUE.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.