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.
สมาคมที่ตรวจสอบช่วงอายุทั้งสองข้าง
- สมาคมรับสมาชิกเฉพาะอายุ 11 ถึง 16 การทดสอบหนึ่งไม่สามารถบอกทั้งสองขอบเขตได้
ANDจะเป็นTRUEก็ต่อเมื่อการทดสอบทั้งสองเป็นTRUE:Age >= 11 AND Age <= 16NOTกลับค่า Boolean หน้าที่เหล่านี้ใช้它与 flag ไม่ใช่กับตัวเลข
DECLARE Age : INTEGER
Age ← 14
IF Age >= 11 AND Age <= 16 THEN
OUTPUT "accepted"
ELSE
OUTPUT "no"
ENDIF
A fare that depends on either age
- A bus fare is free under 5, or at 65 and over. One of those tests is enough.
ORisTRUEwhen either operand isTRUE.
ค่าบริการที่ขึ้นอยู่กับอายุอย่างใดอย่างหนึ่ง
- ค่ารถเมล์ฟรีหากอายุต่ำกว่า 5 หรือ อายุ 65 ขึ้นไป การทดสอบเพียงอย่างหนึ่งก็พอ
ORจะเป็นTRUEเมื่อ operand อย่างใดอย่างหนึ่งเป็นTRUE
DECLARE Age : INTEGER
Age ← 70
IF Age < 5 OR Age >= 65 THEN
OUTPUT "free"
ELSE
OUTPUT "pay"
ENDIF
Nest the second decision
- 80 or more is a distinction. Otherwise, 50 or more is a pass, and the rest is a fail.
- The second
IFis the statement underELSE, with its ownENDIF. THENstays on theIFline, including the inner one.
ซ้อนการตัดสินใจครั้งที่สอง
- 80 ขึ้นไปคือ Distinction ส่วนที่เหลือ 50 ขึ้นไปคือ Pass และส่วนที่เหลือคือ Fail
IFครั้งที่สองคือ statement ภายใต้ELSEพร้อม与之ENDIFของตัวเองTHENยังคงอยู่บนบรรทัดIFรวมถึง inner one ด้วย
DECLARE Mark : INTEGER
Mark ← 72
IF Mark >= 80 THEN
OUTPUT "Distinction"
ELSE
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
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.
ระวัง
- เขียนตัวแปรไว้ทั้งสองข้างของ
ANDMark >= 0 AND <= 100ไม่ใช่การทดสอบ - IGCSE 0478 จะตั้ง
THENไว้ในบรรทัดถัดไป อย่าผสมข้อสอบทั้งสองเข้าด้วยกัน
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.
ข้อผิดพลาดที่พบบ่อย
ELSEยึดติดกับIFด้านบนมัน ขยับ innerIFเข้าไปเพื่อให้ finalENDIFเป็น outer oneNOTใช้กับ BooleanNOT Markไม่ใช่วิธีการทดสอบตัวเลข
Now you try
- One task is an
OR, one is aNOT, and one is the nested grade.
ลองดูเลย
- หนึ่งงานเป็น
OR, หนึ่งงานเป็นNOT, และหนึ่งงานเป็นเกรดที่ซ้อนกัน
Read an INTEGER Age. Output free when Age is under 5 or at least 65, using OR. Otherwise output pay. · อ่าน INTEGER Age. เอาต์พุต free เมื่อ Age ต่ำกว่า 5 หรืออย่างน้อย 65, ใช้ OR. มิฉะนั้นเอาต์พุต pay
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Alarm is a BOOLEAN set to FALSE. If NOT Alarm, output quiet, otherwise output ring. · Alarm เป็น BOOLEAN เซ็ตเพื่อ FALSE. หาก NOT Alarm, ให้แสดงผลลัพธ์ quiet, มิฉะนั้นแสดงผลลัพธ์ ring.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Read an INTEGER Mark. Output Distinction for 80 or more, otherwise Pass for 50 or more, otherwise Fail. Put the second IF under ELSE, with THEN on the IF line. · อ่าน INTEGER Mark. แสดง Distinction สำหรับ 80 ขึ้นไป, มิฉะนั้นแสดง Pass สำหรับ 50 ขึ้นไป, มิฉะนั้นแสดง Fail. วาง IF ตัวที่สองใต้ ELSE, โดยมี THEN บนบรรทัด IF
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่