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.
תוכניות לוקחות החלטות
- בחירה אומרת שהתוכנית בוחרת מה לעשות בהתאם לתנאי.
- תנאי הוא שאלה שמענה לה היא
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מבצע את הבლოק שלו רק כאשר התנאי הואTrue.elif("else if") בודק תנאי נוסף אם הקודמים היוFalse.elseמבצע כאשר אף אחד מתנאי הללו לא היה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.
לוגיקה בולית: and / or / not
andמתהפך ל-Trueרק כאשר שני הצדדים הםTrue.orמתהפך ל-Trueכאשר לפחות אחד מהצדדים הואTrue.notהופך את הערך: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,←==.
בפסאודוקוד AP CSP
- Python
if/elseתואם את ה-IF(condition){ } ELSE { }של CB. - 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 · בחירה בוחרת ענף
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.