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("elif") بفحص شرط آخر إذا كانت الشروط السابقة غير صحيحة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
- بايثون
if/elseتطابق CB'sIF(condition){ } ELSE { }. - تستخدم CB كلمات للتعبير عن المنطق:
AND،OR،NOT. DISPLAY= طباعة،←==.
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.
أخطاء شائعة
- قارن مع
==، لا مع=. - أزل المسافة البادئة للكتلة التي تعمل عندما يكون الشرط صحيحًا.
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. · اضغط تشغيل لرؤية المخرجات هنا.