Logic: AND, OR, NOT
A club that checks both ends of the age
- A club takes members only from 11 to 16. One test cannot say both ends.
ANDisTRUEonly when both tests areTRUE:Age >= 11 AND Age <= 16.NOTflips a Boolean. The tasks use it on a flag, not on a number.
DECLARE Age : INTEGER
Age ← 14
IF Age >= 11 AND Age <= 16 THEN
OUTPUT "accepted"
ELSE
OUTPUT "no"
ENDIF
A day off on either day
- No school on Saturday or Sunday. One of those tests is enough.
ORisTRUEwhen either operand isTRUE. Write the variable in both tests.
DECLARE Day : STRING
Day ← "Sun"
IF Day = "Sat" OR Day = "Sun" THEN
OUTPUT "no school"
ELSE
OUTPUT "school day"
ENDIF
AND, OR and NOT side by side
A |
B |
A AND B |
A OR B |
NOT A |
|---|---|---|---|---|
TRUE |
TRUE |
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
TRUE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
- A
BOOLEANvariable is already a test:IF Raining THENneeds no= TRUE, andIF NOT Raining THENreads "if it is not raining".
DECLARE Raining : BOOLEAN
Raining ← TRUE
IF NOT Raining THEN
OUTPUT "walk"
ELSE
OUTPUT "take the bus"
ENDIF
Nest the second decision
- A speed camera: 120 or more loses the licence. Otherwise, 100 or more is a fine, and the rest is fine to drive.
- The second
IFis the statement underELSE, with its ownENDIF. - Test the highest boundary first. If
Speed >= 100came first, 130 would get only a fine.
DECLARE Speed : INTEGER
Speed ← 105
IF Speed >= 120 THEN
OUTPUT "licence taken"
ELSE
IF Speed >= 100 THEN
OUTPUT "fine"
ELSE
OUTPUT "ok"
ENDIF
ENDIF
Watch out
- Write the variable on both sides of
AND.Mark >= 0 AND <= 100is not a test. - IGCSE 0478 puts
THENon the following line. Do not mix the two papers.
Common mistakes
ELSEbelongs to theIFabove it. Indent the innerIFso the finalENDIFis the outer one.NOTapplies to a Boolean.NOT Markis not how you test a number.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
- One task is an
ORon an age, one is aNOTon a flag, and one is a nested grade with two boundaries. Decide which boundary to test first.
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Read an INTEGER Age. Output free when Age is under 5 or at least 65, using OR. Otherwise output pay.
点击“运行”查看此处输出。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Alarm is a BOOLEAN set to FALSE. If NOT Alarm, output quiet, otherwise output ring.
点击“运行”查看此处输出。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Read an INTEGER Mark. Output Distinction for 80 or more, otherwise Pass for 50 or more, otherwise Fail. Put the second IF 位于 ELSE, with THEN on the IF line.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Read Age (integer) and HasPass (BOOLEAN, entered as TRUE/FALSE). Output enter only when Age is at least 16 AND HasPass is TRUE, otherwise wait.
点击“运行”查看此处输出。