اتخاذ القرارات باستخدام if/else
أنشئ فروعًا بناءً على الشروط
اتخاذ القرارات باستخدام if/else درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Code That Decides
Programs often need to choose a path. The if statement runs a block only when its condition is true, so your code can react to data. 🔀
Your First if
Write if, a condition, a colon, then an indented block. If the condition holds, Mojo runs the block; otherwise it skips it entirely.
if score > 90:
print("Top marks!")Conditions Are Booleans
The bit after if must boil down to True or False. Comparisons like greater-than and equals naturally produce a boolean for you.
var ready = count == 0
if ready:
print("Empty")Add an else Branch
Pair else with if to cover the other case. Exactly one of the two blocks runs, so nothing is ever left unhandled.
if temp > 30:
print("Hot")
else:
print("Mild")Indentation Defines the Block
Like Python, Mojo uses indentation to mark what belongs to a branch. Consistent spaces, usually four, keep the structure clear and valid.
Comparison Operators
Build conditions with operators like ==, !=, <, and >=. Each compares two values and hands back the boolean that if needs.
if age >= 18:
print("Adult")Chain More Cases with elif
Need more than two paths? Insert one or more elif branches between if and else to test extra conditions in order.
if g >= 90:
print("A")
elif g >= 80:
print("B")
else:
print("C")Only the First Match Wins
Mojo checks branches top to bottom and stops at the first condition that is true. The rest are skipped, even if they would also match.
Combine Conditions with and / or
Join checks using and and or. With and both must be true; with or just one is enough to enter the block.
if hungry and has_food:
print("Eat now")Flip a Condition with not
Put not in front of a condition to invert it. It turns True into False, handy for checking the absence of something.
if not logged_in:
print("Please sign in")Nesting Branches
You can place an if inside another branch to refine a decision. Keep nesting shallow so the logic stays easy to read.
if signed_in:
if is_admin:
print("Dashboard")Quick Check
You want to handle three or more separate cases in one decision. Which keyword do you reach for?
Recap
You branch with if, add else for the other case, and chain elif for more. Conditions are booleans you can combine with and, or, and not. 🎯
الأسئلة الشائعة
هل درس «اتخاذ القرارات باستخدام if/else» مجاني؟
نعم — نص درس «اتخاذ القرارات باستخدام if/else» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «اتخاذ القرارات باستخدام if/else»؟
أنشئ فروعًا بناءً على الشروط تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «اتخاذ القرارات باستخدام if/else»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اتخاذ القرارات باستخدام if/else
- التكرار باستخدام while
- التكرار باستخدام for وrange
- break وcontinue والخروج المبكر