Logic: AND, OR, NOT · 逻辑:AND、OR 与 NOT
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
A club that checks your age
- A coding club takes members only from age 11 to 16. One test cannot say both ends.
ANDisTRUEonly when both tests areTRUE.- Write the variable on each side:
Age >= 11 AND Age <= 16.
中文
社团要核对年龄
- 编程社团只收 11 到 16 岁。一个判断说不了两端。
- 只有两个判断都为
TRUE时,AND才为TRUE。 - 每一边都写上变量:
Age >= 11 AND Age <= 16。
DECLARE Age : INTEGER
Age ← 14
IF Age >= 11 AND Age <= 16
THEN
OUTPUT "accepted"
ELSE
OUTPUT "no"
ENDIF
English
OR: either test is enough
- A bus fare is free for a child or a senior: under 5, or 65 and over.
ORisTRUEwhen at least one test isTRUE.
中文
OR:任一判断就够
- 儿童或老年人免票:不满 5 岁,或年满 65 岁。
- 至少有一个判断为
TRUE时,OR就为TRUE。
DECLARE Age : INTEGER
Age ← 70
IF Age < 5 OR Age >= 65
THEN
OUTPUT "free"
ELSE
OUTPUT "pay"
ENDIF
English
NOT flips a Boolean
NOTturnsTRUEintoFALSE, andFALSEintoTRUE.- A search that has not found the target yet is
NOT Found.
中文
NOT 把布尔值反过来
NOT把TRUE变成FALSE,把FALSE变成TRUE。- 查找还没找到目标时,就是
NOT Found。
DECLARE Found : BOOLEAN
Found ← FALSE
IF NOT Found
THEN
OUTPUT "missing"
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).
中文
注意
Mark >= 0 AND <= 100不是一个判断。后一半没有被比较的东西。- 混在一起时用括号写清楚:
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.
中文
常见错误
AND、OR、NOT是关键字。它们的运算数是BOOLEAN,通常来自一个关系判断。9618把THEN放在IF那一行。这门课把THEN放在下一行。
English
Now you try
- Each task is one decision. Write both sides of
ANDand ofOR. - Press Check answer to test it.
中文
现在你来试
- 每道题是一个判断。
AND和OR的两边都要写上。 - 按检查答案来测试。
Read an INTEGER Age. Output accepted when it is from 11 to · 到 16 inclusive, using AND. Otherwise output no. · 读入一个 INTEGER Age。用 AND 判断它是否在 11 到 16(含两端)。是则输出 accepted,否则输出 no。
Click Run to see the output here. · 点击“运行”查看此处输出。
Read an INTEGER Age. Output free when Age is under 5 or at least 65, using OR. Otherwise output pay. · 读入一个 INTEGER Age。用 OR 判断 Age 是否小于 5 或不小于 65。是则输出 free,否则输出 pay。
Click Run to see the output here. · 点击“运行”查看此处输出。
Found is a BOOLEAN set to FALSE. If NOT Found, output missing. · Found 是 BOOLEAN,设为 FALSE。若 NOT Found,输出 missing。
Click Run to see the output here. · 点击“运行”查看此处输出。