Logic: AND, OR, NOT
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
A room that stays comfortable
- A thermostat keeps a room between 18 and 24 degrees. One test cannot say both ends.
ANDisTRUEonly when both tests areTRUE.- Write the variable on each side:
Temp >= 18 AND Temp <= 24.

DECLARE Temp : INTEGER
Temp ← 21
IF Temp >= 18 AND Temp <= 24
THEN
OUTPUT "comfortable"
ELSE
OUTPUT "adjust the heating"
ENDIF
- Both ends are included because the tests use
>=and<=. With>and<, 18 and 24 would fail.
OR: either test is enough
- A school closes on a holiday or when snow is deeper than 20 cm. One reason is enough.
ORisTRUEwhen at least one test isTRUE.
DECLARE Holiday : BOOLEAN
DECLARE Snow : INTEGER
Holiday ← FALSE
Snow ← 25
IF Holiday OR Snow > 20
THEN
OUTPUT "school closed"
ELSE
OUTPUT "school open"
ENDIF
Holiday |
Snow > 20 |
Holiday OR Snow > 20 |
Holiday AND Snow > 20 |
|---|---|---|---|
TRUE |
TRUE |
TRUE |
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
NOT flips a Boolean
NOTturnsTRUEintoFALSE, andFALSEintoTRUE.- A
BOOLEANvariable is already a test: writeIF Raining, notIF Raining = TRUE, andIF NOT Rainingfor the opposite.
DECLARE Raining : BOOLEAN
Raining ← FALSE
IF NOT Raining
THEN
OUTPUT "walk to school"
ELSE
OUTPUT "take the bus"
ENDIF
Watch out
Mark >= 0 AND <= 100is not a test. The second half has nothing to compare.- Parentheses make a mix obvious:
NOT (Age < 11 OR Age > 16).
Common mistakes
AND,ORandNOTare keywords. Their operands areBOOLEAN, usually from a relational test.9618putsTHENon theIFline. This course keepsTHENon the next line.Day = "Sat" OR "Sun"is not a test: writeDay = "Sat" OR Day = "Sun".ANDwhere you meantOR: no age is under 5 and 65 or over at the same time, so that test is neverTRUE.
Now you try
- Each task is one decision. Write both sides of
ANDand ofOR. - Press Check answer to test it.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Read an INTEGER Age. Output accepted when it is from 11 to 16 inclusive, using AND. Otherwise output no.
Click Run to see the output here.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Read an INTEGER Age. Output free when Age is under 5 or at least 65, using OR. Otherwise output pay.
Click Run to see the output here.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Found is a BOOLEAN set to FALSE. If NOT Found, output missing.
Click Run to see the output here.