Unit 3: Programming · Conditionals

Article

AND, OR and NOT

7 min read

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 true is false, and NOT false is true.
  • 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:

ABA AND BA OR B
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse
ANOT A
truefalse
falsetrue

Python writes the three operators in lowercase: and, or, not. AP pseudocode writes them in capitals.

Runs in your browser

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.

AP pseudocode
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:

  1. Do anything in brackets first.
  2. Then NOT.
  3. Then AND.
  4. 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)

StepPartResult
1x < 5 is 7 < 5false
2NOT (x < 5) is NOT falsetrue
3y = 2 OR y = 3true OR false, which is true
4true AND truetrue

The whole expression is true.

Check your understanding

What is displayed when this line runs?

AP pseudocode
DISPLAY(NOT (3 > 5))

Worked example: a trace

AP pseudocode
temp ← 20
sunny ← false
IF(temp ≥ 20 AND sunny)
{
    DISPLAY("Beach")
}
ELSE
{
    IF(temp ≥ 20 OR sunny)
    {
        DISPLAY("Park")
    }
    ELSE
    {
        DISPLAY("Home")
    }
}
StepTestResult
1temp ≥ 20 AND sunny is true AND falsefalse, so go to the ELSE block
2temp ≥ 20 OR sunny is true OR falsetrue, so show Park

The program displays Park.

Common mistakes

  • Using OR when you mean AND for a range. n ≥ 10 OR n ≤ 20 is 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, not x = 3 OR 4.
  • Dropping the brackets. NOT (a AND b) and NOT a AND b are 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 < 20 leaves 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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.