Nested decisions
One mark, three outcomes
- A mark of 80 or more is a distinction, 50 or more is a pass, and anything lower is a fail.
- One
IFhas two paths. The second decision goes inside theELSE, as its ownIF. - Each
IFhas its ownENDIF. Indent the inner one so you can see whichELSEit belongs to.
คะแนนหนึ่งจุด ผลลัพธ์สามแบบ
- คะแนน 80 ขึ้นไป คือ Distinction, 50 ขึ้นไป คือ Pass และต่ำกว่านั้นคือ Fail
- One
IFมีสองเส้นทาง การตัดสินใจครั้งที่สองอยู่ ภายในELSEในฐานะIFของตัวเอง - แต่ละ
IFมีENDIFของตัวเอง Indent ตัวในเพื่อให้คุณรู้ว่า **ELSE**ไหนที่มันสังกัด
DECLARE Mark : INTEGER
Mark ← 72
IF Mark >= 80
THEN
OUTPUT "Distinction"
ELSE
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
Count the marks that count
- A validation tally walks the list and adds 1 only when the value is in range.
- The range test is the
ANDfrom the previous lesson, inside the loop.
นับคะแนนที่มีผล
- การนับตรวจสอบ (validation tally) จะวนผ่านรายการและบวก 1 ก็ต่อเมื่อค่านั้นอยู่ในช่วงที่กำหนด
- การทดสอบช่วงคือ
ANDจากบทเรียนก่อนหน้า อยู่ภายในลูป
DECLARE Mark : ARRAY[1:4] OF INTEGER
DECLARE I, Count : INTEGER
Mark[1] ← 80
Mark[2] ← -1
Mark[3] ← 100
Mark[4] ← 40
Count ← 0
FOR I ← 1 TO 4
IF Mark[I] >= 0 AND Mark[I] <= 100
THEN
Count ← Count + 1
ENDIF
NEXT I
OUTPUT Count // → 3
Watch out
- The inner
IFstarts on the line afterELSE. It is not written on theELSEline. - Close the inner
ENDIFbefore the outer one. The lastENDIFbelongs to the firstIF.
ระวัง
IFภายในเริ่มบรรทัด หลังจากELSEมันไม่ได้เขียนบนบรรทัดELSE- ปิด
ENDIFภายในก่อนตัวนอก LastENDIFสังกัดกับIFแรก
Common mistakes
- An
ENDIFin the wrong place closes the outer test and leaves the inner one open. - A mark of exactly 50 is a pass, and exactly 80 is a distinction. The tests use
>=.
ข้อผิดพลาดที่พบบ่อย
ENDIFที่ผิดตำแหน่งจะปิดการทดสอบภายนอกและทำให้ภายในเปิดค้างไว้- คะแนน 50 พอดีคือ Pass และ 80 พอดีคือ Distinction การทดสอบใช้
>=
Now you try
- The first two tasks are nested decisions. The third counts with
AND. - Press Check answer to test it.
ลองดูเลย
- สองงานแรกเป็นการตัดสินใจซ้อนกัน งานที่สามนับด้วย
AND - กด Check answer เพื่อทดสอบ
Read an INTEGER Mark. Output Distinction for 80 or more, otherwise Pass for 50 or more, otherwise Fail. Nest the second IF under ELSE. · อ่าน INTEGER Mark. แสดงผล Distinction สำหรับคะแนน 80 ขึ้นไป, ถ้าไม่แสดงผล Pass สำหรับคะแนน 50 ขึ้นไป, ถ้าไม่แสดงผล Fail. จัดรูปแบบการซ้อนของ IF-under ELSE.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Same three outcomes as the previous task. This input should take the inner ELSE. · ผลลัพธ์สามอย่างเดิมจากงานก่อนหน้า. อินพุตนี้ควรรับ ELSE ภายใน
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The array Mark holds 80, -1, 100, 40. Count how many values are from 0 to 100 inclusive and output the count (it is 3). · อาร์เรย์ Mark เก็บค่า 80, -1, 100, 40 นับว่ามีค่าใดบ้างที่มีค่าตั้งแต่ 0 ถึง 100 (รวมขอบเขต) และแสดงผลจำนวนนับ (ซึ่งมีค่า 3)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่