AND, OR, NOT — Pseudocode

AND, OR and NOT join several comparisons in one condition. AND is TRUE only when the comparisons on both of its sides are TRUE. OR is TRUE when at least one…

The door of Mrs. Sri's storeroom has a digital lock. The door opens only when two things are true at the same time: the card is tapped, and the PIN matches. A card without the PIN is not enough. The PIN without the card is not enough either. So if someone finds the card in the street, the storeroom stays safe.

One comparison can only check one thing. To join two or more comparisons in one condition, there are three words: AND, OR and NOT.

AND: both have to be TRUE

Here is the storeroom lock as a program, with its result below the code. card holds "yes" when the card is tapped, and the correct PIN is 4821:

card ← "yes"
pin ← 4821
IF card = "yes" AND pin = 4821 THEN
    PRINT "Door open"
ELSE
    PRINT "Door stays locked"
END IF
Door open

AND joins two comparisons: one on its left side and one on its right side. The result of AND is TRUE only when both sides are TRUE. If you change the second line to pin ← 1234, one side is FALSE, so the result is Door stays locked.

OR: one is enough

This program gives a weekend discount, on Saturday or Sunday:

day ← "Sunday"
IF day = "Saturday" OR day = "Sunday" THEN
    PRINT "Weekend discount"
ELSE
    PRINT "Normal price"
END IF
Weekend discount

The result of OR is TRUE when one side is TRUE, or when both are. It is FALSE only when both sides are FALSE. Notice also that day is written twice. Each side of OR must be a complete comparison.

The truth table

All possible results of AND and OR fit in one table, called a truth table. Say a is "the card is tapped" and b is "the PIN matches":

a b a AND b a OR b
TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE
FALSE TRUE FALSE TRUE
FALSE FALSE FALSE FALSE

You can check the table yourself, because TRUE and FALSE can be written directly in a program:

PRINT TRUE AND FALSE, TRUE OR FALSE
PRINT FALSE OR FALSE
FALSE TRUE
FALSE

NOT: flipping

NOT is written in front of a value that is TRUE or FALSE. In this program, raining holds FALSE:

raining ← FALSE
IF NOT raining THEN
    PRINT "Dry the crackers outside"
END IF
Dry the crackers outside

NOT flips TRUE to FALSE, and FALSE to TRUE. So NOT raining is TRUE, and the crackers are dried outside. In front of a comparison, NOT flips the result of that comparison:

stock ← 0
PRINT NOT stock > 0
TRUE

NOT stock > 0 is read as NOT (stock > 0): the stock is not above zero, which means none is left. Because stock holds 0, the result is TRUE.

Which goes first

AND is calculated before OR, like * is calculated before +. Sometimes that order is not what you mean. Say Mrs. Sri's discount is only for Sunday, for members (registered customers) or customers who spend more than 100:

member ← TRUE
total ← 20
day ← "Monday"
PRINT member OR total > 100 AND day = "Sunday"
PRINT (member OR total > 100) AND day = "Sunday"
TRUE
FALSE

In this program the day is Monday, so there should be no discount. But the first PRINT line prints TRUE (a discount). The computer reads it as member OR (total > 100 AND day = "Sunday"), and member is already TRUE. In the second PRINT line, the brackets make the computer calculate OR first, so the result is FALSE. When you are not sure, use brackets.

Flipping AND into OR

The storeroom door opens when the card AND the PIN are right. When does the door stay locked? Not only when both are wrong. One wrong thing is enough: the card is missing, OR the PIN is wrong. The two PRINT lines below write "the door stays locked" in two ways:

card ← "yes"
pin ← 1234
PRINT NOT (card = "yes" AND pin = 4821)
PRINT card ≠ "yes" OR pin ≠ 4821
TRUE
TRUE

The two PRINT lines always give the same result, whatever card and pin hold. The rule: when you flip a condition with AND, AND becomes OR, and each comparison is flipped too (= becomes ≠). In the Boolean Logic lesson later, this rule is called De Morgan's law. In JavaScript, AND, OR, NOT are written &&, ||, !. In Python, and, or, not.

⚠️ Everyday sentences with a missing part

One mistake causes no error: writing AND when you mean OR. day = "Saturday" AND day = "Sunday" is always FALSE, because no day is both Saturday and Sunday. The program runs, but the discount is never given.

TL;DR: AND, OR and NOT join several comparisons in one condition. AND is TRUE only when the comparisons on both of its sides are TRUE. OR is TRUE when at least one side is TRUE. NOT flips TRUE to FALSE, and FALSE to TRUE. Each side of AND and OR must be a complete comparison: day = "Saturday" OR day = "Sunday". AND is calculated before OR, so use brackets when you mean something else. When a and b are two comparisons, the opposite of a AND b is NOT a OR NOT b.

⚡ AND: both have to be true

Write:

card ← "yes"
pin ← 4821
IF card = "yes" AND pin = 4821 THEN
    PRINT "Door open"
ELSE
    PRINT "Door stays locked"
END IF
Guess first: this program opens the door when the card is tapped AND the PIN is 4821. If the second line becomes pin ← 1234 and the card is still "yes", does the door open?

No, Door stays locked is printed. One false side is enough to make the whole AND FALSE.

Meaning: AND joins two comparisons, one on its left side and one on its right side. Its result is TRUE only when both sides are TRUE.

⚡ OR: one is enough

Write:

day ← "Sunday"
IF day = "Saturday" OR day = "Sunday" THEN
    PRINT "Weekend discount"
ELSE
    PRINT "Normal price"
END IF
Guess first: can the condition be shortened to IF day = "Saturday" OR "Sunday" THEN?

No, the computer stops with an error. The right side of OR is only the text "Sunday", not a comparison that gives TRUE or FALSE.

Meaning: The result of OR is TRUE when one of its sides is TRUE, and FALSE only when both sides are FALSE. Each side must be a complete comparison, so day is written twice.

⚡ NOT flips

Meaning: NOT flips TRUE to FALSE, and FALSE to TRUE. NOT stock > 0 is read as NOT (stock > 0): the stock is not above zero, which means none is left.

⚡ AND goes first

Meaning: AND is calculated before OR, like * is calculated before +. So member OR total > 100 AND day = "Sunday" is read as member OR (total > 100 AND day = "Sunday"). Here member holds TRUE for a registered customer. When you mean something else, or you are not sure, use brackets.

What you will learn