Article
AND, OR and NOT
A club says: "You can come in if you are 13 or older AND you have a ticket." That is two conditions that must both be true. Other rules need only one of two. A program can join conditions together, using three small words.
NOT, AND, OR
A logical operator works on Boolean values and gives a Boolean. There are three of them.
- NOT turns a Boolean around.
NOT trueisfalse, andNOT falseistrue. - AND is true only when both sides are true.
- OR is true when at least one side is true. That includes the case when both are true.
In everyday talk, "tea or coffee" often means one of them. In logic, OR is more generous: if both sides are true, the result is still true.
A truth table lists every possible pair of inputs and the result. With two inputs, A and B, there are four rows:
| A | B | A AND B | A OR B |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
| A | NOT A |
|---|---|
| true | false |
| false | true |
Python writes the three operators in lowercase: and, or, not. AP pseudocode writes them in capitals.
The output is False, True and False. Check each one against the tables, then change the values of a and b and predict the result before you run it.
Check your understanding
For which values of A and B is A AND B true?
Joining comparisons
The two sides of AND and OR are usually comparisons. Each side must be a full Boolean expression on its own.
age ← 15
hasTicket ← true
IF(age ≥ 13 AND hasTicket)
{
DISPLAY("Enter")
}
ELSE
{
DISPLAY("Stop")
}age ≥ 13 is true and hasTicket is true, so the whole condition is true. The output is Enter.
AND is the right tool for a range. A number from 10 to 20, including both ends, is n ≥ 10 AND n ≤ 20. OR is the right tool for outside a range: n < 10 OR n > 20.
Do not shorten the condition. x = 3 OR 4 is not allowed. You must write the whole comparison on each side: x = 3 OR x = 4.
Working out a long expression
When an expression has several parts, work it out in small steps:
- Do anything in brackets first.
- Then NOT.
- Then AND.
- Then OR.
This is the order Python uses. The AP exam's reference sheet doesn't set an order for NOT, AND and OR, so exam questions use brackets wherever the order matters: work from the inside out. Take x ← 7 and y ← 2:
NOT (x < 5) AND (y = 2 OR y = 3)
| Step | Part | Result |
|---|---|---|
| 1 | x < 5 is 7 < 5 | false |
| 2 | NOT (x < 5) is NOT false | true |
| 3 | y = 2 OR y = 3 | true OR false, which is true |
| 4 | true AND true | true |
The whole expression is true.
Check your understanding
What is displayed when this line runs?
DISPLAY(NOT (3 > 5))
Worked example: a trace
temp ← 20
sunny ← false
IF(temp ≥ 20 AND sunny)
{
DISPLAY("Beach")
}
ELSE
{
IF(temp ≥ 20 OR sunny)
{
DISPLAY("Park")
}
ELSE
{
DISPLAY("Home")
}
}| Step | Test | Result |
|---|---|---|
| 1 | temp ≥ 20 AND sunny is true AND false | false, so go to the ELSE block |
| 2 | temp ≥ 20 OR sunny is true OR false | true, so show Park |
The program displays Park.
Common mistakes
- Using OR when you mean AND for a range.
n ≥ 10 OR n ≤ 20is true for every number. Use AND. - Reading OR as "exactly one". OR is also true when both sides are true.
- Shortening a condition. Write
x = 3 OR x = 4, notx = 3 OR 4. - Dropping the brackets.
NOT (a AND b)andNOT a AND bare different expressions. - Using capitals in Python. Python wants
and,or,not. The capitals are only for AP pseudocode. - Forgetting the edge values.
n > 10 AND n < 20leaves out 10 and 20.
On the exam
You will evaluate compound expressions for given values, choose the condition that matches a rule in words, and read truth tables. Write each part's value above it and combine them one pair at a time. When a rule says "from 10 to 20", check what happens at 10 and at 20.
Key words
Logical operator: NOT, AND or OR.
Compound Boolean expression: an expression that joins Boolean expressions with logical operators.
Truth table: a table that shows the result for every combination of true and false inputs.