Selection — IF and CASE · الاختيار — IF وCASE
Making a decision
- Selection means choosing what to do, based on a test.
- An
ifruns its block only when the test is true. - The test gives a Boolean:
TrueorFalse.
اتخاذ قرار
- الاختيار (Selection) يعني اختيار ما يجب فعله بناءً على اختبار.
- كتلة
ifتعمل فقط عندما يكون الاختبار صحيحًا. - يعطي الاختبار قيمة منطقية:
TrueأوFalse.
age = 20
if age >= 18:
print("You may vote")
else: the other path
elseruns when theiftest is false.- Exactly one of the two blocks runs.
else: المسار الآخر
elseتعمل عندما يكون اختبارifخاطئًا.- تعمل إحدى الكتلتين بالضبط.
mark = 45
if mark >= 50:
print("Pass")
else:
print("Fail")
elif: many paths
- Use
elifto test more conditions in order. - Python checks each test top to bottom and stops at the first true one.
elif: مسارات متعددة
- استخدم
elifلاختبار المزيد من الشروط بالترتيب. - يتحقق Python من كل اختبار من الأعلى إلى الأسفل ويتوقف عند الأول الصحيح.
mark = 72
if mark >= 70:
print("A")
elif mark >= 50:
print("B")
else:
print("C")
if / elif / else — which runs? · if / elif / else — أيهم يعمل؟
Tests run top-down; the first true one runs, the rest are skipped. · الاختبارات تُنفَّذ من الأعلى للأسفل؛ أول واحد صحيح يعمل، والباقي يتم تخطيه.
Comparison operators
==means equal to (the exam writes=).!=means not equal to (the exam writes<>).- Also use
<,>,<=,>=.
مشغلات المقارنة
==تعني يساوي (يكتب الامتحان=).!=تعني لا يساوي (يكتب الامتحان<>).- استخدم أيضًا
<،>،<=،>=.
print(5 == 5) # True
print(5 != 3) # True
print(3 <= 2) # False
In the exam: IF and CASE
- Pseudocode uses
IF … THEN … ELSE … ENDIF, orCASEfor many fixed choices.
في الامتحان: IF وCASE
- تستخدم الخوارزميات الوهمية
IF … THEN … ELSE … ENDIF، أوCASEللعديد من الخيارات الثابتة.
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Common mistakes
- Compare with
==, not=. - Cover every case, including the
else. - Indent the block that belongs to each branch.
أخطاء شائعة
- قارن مع
==، لا مع=. - غطِ كل حالة، بما في ذلك
else. - قم بمسافة بادئة للكتلة التابعة لكل فرع.
Now you try
- Read the input, then choose what to print with
if/elif/else. - Press Check answer to test your code.
الآن جرب بنفسك
- اقرأ المدخلات، ثم اختر ما تطبعه باستخدام
if/elif/else. - اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
Read a whole number, then print even if it divides by 2 with no remainder, otherwise print odd. For input 4, print even. · اقرأ رقماً صحيحاً، ثم اطبع even إذا كان يقبل القسمة على 2 بدون باقٍ، وإلا اطبع odd. بالنسبة للإدخال 4، اطبع even.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Read a mark (0–100). Print A for 70 or more, B for 50–69, and C for anything less. For input 65, print B. · اقرأ علامة (0–100). اطبع A لـ 70 فأكثر، B لـ 50–69، وC لأي أقل. بالنسبة للإدخال 65، اطبع B.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Two numbers a and b are set for you. Use an if to store the larger one in biggest. · رقمان a وb مُعيّنان لك. استخدم if لحفظ الرقم الأكبر في biggest.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.