Selection · การคัดเลือก
Programs make choices
- Selection means the program picks what to do based on a condition.
- A condition is a question that is
TrueorFalse. - This is a key idea in AP CSP: code that decides.
โปรแกรมเลือกสิ่งที่จะทำ
- Selection หมายถึงโปรแกรมเลือกสิ่งที่要做ตามเงื่อนไข
- เงื่อนไขคือคำถามที่เป็น
TrueหรือFalse - นี่คือแนวคิดสำคัญใน AP CSP: โค้ดที่ ตัดสินใจ
if / elif / else
ifruns its block only when the condition isTrue.elif("else if") checks another condition if the ones above wereFalse.elseruns when none of the conditions wereTrue.
if / elif / else
ifทำงานเฉพาะเมื่อเงื่อนไขเป็นTrueelif("else if") ตรวจสอบเงื่อนไขอื่นหากเงื่อนไขด้านบนเป็นFalseelseทำงานเมื่อไม่มีเงื่อนไขใดเป็นTrueเลย
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
Comparisons make conditions
==equal,!=not equal,<><=>=.==tests if two values are equal; a single=stores a value.- The result of a comparison is
TrueorFalse.
การเปรียบเทียบสร้างเงื่อนไข
==เท่ากับ,!=ไม่เท่ากับ,<><=>===ทดสอบ ว่าสองค่าเท่ากันหรือไม่;符号=เก็บ ค่า- ผลลัพธ์ของการเปรียบเทียบคือ
TrueหรือFalse
score = 72
print(score >= 50)
print(score == 100)
Boolean logic: and / or / not
andisTrueonly when both sides areTrue.orisTruewhen at least one side isTrue.notflips it:not TrueisFalse.
ตรรกะ Boolean: and / or / not
andisTrueonly when both sides areTrueorisTruewhen at least one side isTruenotกลับค่า:not Trueเป็นFalse
age = 16
if age >= 13 and age <= 19:
print("Teenager")
else:
print("Not a teenager")
In AP CSP pseudocode
- Python
if/elsematches CB'sIF(condition){ } ELSE { }. - CB uses words for logic:
AND,OR,NOT. DISPLAY= print,←==.
In AP CSP pseudocode
- Python
if/elseตรงกับ CB'sIF(condition){ } ELSE { } - CB ใช้คำสำหรับตรรกะ:
AND,OR,NOT DISPLAY= print,←==
IF (score >= 50)
{
DISPLAY "Pass"
}
ELSE
{
DISPLAY "Fail"
}
IF ((age >= 13) AND (age <= 19))
{
DISPLAY "Teenager"
}
Common mistakes
- Compare with
==, not=. - Indent the block that runs when the condition is true.
ข้อผิดพลาดที่พบบ่อย
- เปรียบเทียบกับ
==, ไม่ใช่= - Indent บล็อกที่ทำงานเมื่อเงื่อนไขเป็นจริง
Now you try
- Read each task to see what to print or check.
- Match the output text exactly.
ลองดูเลย
- อ่านแต่ละโจทย์เพื่อดูว่าต้องพิมพ์อะไรหรือตรวจสอบอะไร
- ตรงกับข้อความผลลัพธ์ อย่างถูกต้อง
Selection picks a branch · Selection เลือกเส้นทางหนึ่ง
Conditions are tested in order; the first true one runs. · เงื่อนไขถูกทดสอบตามลำดับ; เงื่อนไขที่ เป็นจริงตัวแรก จะ被执行
Read a score with int(input()). Print A if it is 80 or more, B if it is 60 or more, otherwise C. Use if / elif / else. For input 75, print B. · อ่านคะแนนด้วย int(input()) พิมพ์ A ถ้าเป็น 80 ขึ้นไป, B ถ้าเป็น 60 ขึ้นไป, มิฉะนั้นพิมพ์ C ใช้ if / elif / else สำหรับอินพุต 75 ให้พิมพ์ B
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
temp is 22. Set comfortable to True when temp is from 18 to 25 (inclusive), else False. Use one comparison with and. · temp คือ 22 ตั้งค่า comfortable เป็น True เมื่อ temp อยู่ในช่วง 18 ถึง 25 (รวม), มิฉะนั้นเป็น False ใช้การเปรียบเทียบหนึ่งครั้งกับ and
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
day is "Sun" and is_holiday is False. Print Day off if it is a weekend ("Sat" or "Sun") or a holiday; otherwise print Work day. Use or. · day คือ "Sun" และ is_holiday คือ False พิมพ์ Day off หากเป็นวันหยุดสุดสัปดาห์ ("Sat" หรือ "Sun") หรือ วันหยุด; มิฉะนั้นพิมพ์ Work day ใช้ or
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่