Logic: AND, OR, NOT
Esta página necesita un navegador reciente (con soporte para SharedArrayBuffer). Por favor, actualiza Chrome, Edge, Firefox o Safari a la última versión.
A club that checks your age
- 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.
English
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.
English
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.
English
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 |
English
NOT flips a Boolean
NOTturnsTRUEintoFALSE, andFALSEintoTRUE.- A
BOOLEANvariable is already a test: writeIF Raining, notIF Raining = TRUE, andIF NOT Rainingfor the opposite.
English
DECLARE Raining : BOOLEAN
Raining ← FALSE
IF NOT Raining
THEN
OUTPUT "walk to school"
ELSE
OUTPUT "take the bus"
ENDIF
English
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).
English
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.
English
Now you try
- Each task is one decision. Write both sides of
ANDand ofOR. - Press Check answer to test it.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Read an INTEGER Age. Output accepted when it is from 11 hasta 16 inclusive, using AND. Otherwise output no.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Read an INTEGER Age. Output free when Age is under 5 or at least 65, using OR. Otherwise output pay.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Found is a BOOLEAN set to FALSE. If NOT Found, output missing.
Haz clic en Ejecutar para ver la salida aquí.