AP® Computer Science Principles · Big Idea 3 · Topic 3.5

Logical Operators NOT, AND, and OR

NOT, AND, and OR are the three basic ways to combine or flip Boolean values, each defined by a truth table that says what the result is for every combination of true and false inputs.

Each operator does one specific job. NOT flips a single value: true becomes false, false becomes true. AND looks at two values and gives true only when both are true; any false makes the whole thing false. OR gives true when at least one value is true, and only fails when both are false. These rules are exactly why an alarm wired with AND (motion sensor AND door sensor) stays quiet unless both trip, while one wired with OR (motion sensor OR door sensor) goes off if either does. AND is strict and demanding; OR is generous and easily satisfied.

You build a truth table by listing every combination of true/false for the inputs, then applying the rule row by row. For A AND (B OR NOT A), you work inside out: compute NOT A, then B OR NOT A, then AND that with A. With two inputs there are four rows to check, and the final column is the answer for every possible case.

This connects directly to branching: an if statement's condition is a Boolean expression, often built from AND, OR, and NOT, and the program takes different paths depending on what that expression evaluates to. Getting these operators right is also what makes De Morgan's law work, which lets you rewrite a negated compound condition into an equivalent one, something that matters when simplifying or debugging conditions in code.

Key terms

Boolean value
A value that is either true or false, with no other option.
Truth table
A table listing every combination of input values alongside the resulting output for a logical expression.
AND
A logical operator that is true only when both of its inputs are true.
OR
A logical operator that is true when at least one of its inputs is true.
De Morgan's law
The rule that NOT (A AND B) equals (NOT A) OR (NOT B), and NOT (A OR B) equals (NOT A) AND (NOT B).

Watch it explained

  1. How it worksHow do NOT, AND, and OR each build a truth table out of Boolean inputs?
  2. Worked exampleFind the value of (x > 5) AND NOT (y = 2) when x is 8 and y is 2.
  3. Worked exampleShow me the complete truth table for A AND (B OR NOT A).
  4. Common mistakeWhy isn't NOT (A AND B) the same as (NOT A) AND (NOT B)?

Check yourself

Your turn1 of 2
Out of 60 test days, sensor A triggers on 20 of them and sensor B triggers on 20 of them, with the two overlapping on 8 days. An alarm wired with OR goes off whenever A or B (or both) triggers. On how many of the 60 days does the OR-wired alarm go off?

End of topic.