Selection · Sự lựa chọn
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.
Chương trình đưa ra quyết định
- Lựa chọn có nghĩa là chương trình chọn việc phải làm dựa trên một điều kiện.
- Điều kiện là một câu hỏi có kết quả là
TruehoặcFalse. - Đây là ý tưởng cốt lõi trong AP CSP: mã code quyết định hành động.
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
ifchạy khối mã của nó chỉ khi điều kiện làTrue.elif("else if") kiểm tra một điều kiện khác nếu các điều kiện phía trên làFalse.elsechạy khi không có điều kiện nào là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.
So sánh tạo điều kiện
==bằng nhau,!=không bằng nhau,<><=>=.==kiểm tra xem hai giá trị có bằng nhau không; một dấu=đơn lẻ lưu một giá trị.- Kết quả của phép so sánh là
TruehoặcFalse.
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.
LogicBoolean: and / or / not
andchỉ làTruekhi cả hai vế đềuTrue.orlàTruekhi ít nhất một vế làTrue.notđảo ngược nó:not Truetrở thànhFalse.
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,←==.
Trong伪代码 của AP CSP
- Python
if/elsekhớp với CB'sIF(condition){ } ELSE { }. - CB sử dụng từ ngữ cho logic:
AND,OR,NOT. DISPLAY= in,←==.
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.
Lỗi thường gặp
- So sánh với
==, không phải với=. - Thuê thụt khối mã chạy khi điều kiện đúng.
Now you try
- Read each task to see what to print or check.
- Match the output text exactly.
Bây giờ bạn thử
- Đọc kỹ mỗi bài tập để biết cần in gì hoặc kiểm tra gì.
- Khớp chính xác với văn bản đầu ra.
Selection picks a branch · Lựa chọn chọn một nhánh
Conditions are tested in order; the first true one runs. · Các điều kiện được kiểm tra theo thứ tự; điều kiện đúng đầu tiên sẽ được thực thi.
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. · Đọc điểm số với int(input()). In A nếu nó lớn hơn hoặc bằng 80, in B nếu nó lớn hơn hoặc bằng 60, ngược lại in C. Sử dụng if / elif / else. Với đầu vào 75, in ra B.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
temp is 22. Set comfortable to True when temp is from 18 to 25 (inclusive), else False. Use one comparison with and. · temp là 22. Gán comfortable bằng True khi temp nằm trong khoảng từ 18 đến 25 (bao gồm cả hai đầu), ngược lại gán là False. Sử dụng một phép so sánh với and.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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 là "Sun" và is_holiday là False. In Day off nếu đó là ngày cuối tuần ("Sat" hoặc "Sun") hoặc là ngày lễ; ngược lại in Work day. Sử dụng or.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.