Skip to content
العربية

4. Conditions

To choose between two paths with اذا (if) and غير ذلك (else).

  • A condition is a question whose answer is صواب (true) or خطاء (false).
  • An اذا line ends with a colon :. The lines indented under it form the block, which runs if the condition holds. Each indentation level is 4 spaces.
  • غير ذلك اذا (else if) is an alternative condition, and غير ذلك covers everything left.
Example
درجة = 75
اذا درجة >= 90:
اطبع("ممتاز")
غير ذلك اذا درجة >= 60:
اطبع("ناجح")
غير ذلك:
اطبع("راسب")
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
ناجح

Try this example in the playground

Forgetting the ::

Causes the error
س = 3
اذا س > 2
اطبع(س)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
​[خطأ نحوي E0019] سطر 2:10 - انتهى السطر بشكل مفاجئ: متوقع ':'   2 | اذا س > 2     |          ^  تلميح: هل نسيت ':' في نهاية سطر الشرط؟

Exit code: 2

In the error catalog: E0019

Try this example in the playground

Fixes it
س = 3
اذا س > 2:
اطبع(س)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
3

Try this example in the playground

Or uneven indentation:

Causes the error
اذا صواب:
اطبع(1)
اطبع(2)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
​[خطأ لفظي E0006] سطر 3:1 - المسافة البادئة (2) لا تطابق أي مستوى سابق   3 |   اطبع(2)     | ^~  تلميح: للعودة إلى كتلة سابقة استخدم نفس عدد مسافاتها تماماً (المستويات المفتوحة الآن: 4 أو 0).

Exit code: 2

In the error catalog: E0006

Try this example in the playground

Fixes it
اذا صواب:
اطبع(1)
اطبع(2)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
12

Try this example in the playground

Turn a grade (85) into a rating: 90 or more is «ممتاز» (excellent), 75 or more «جيد جداً» (very good), 50 or more «ناجح» (pass), and anything below «راسب» (fail).

Show the solution
Solution
درجة = 85
اذا درجة >= 90:
اطبع("ممتاز")
غير ذلك اذا درجة >= 75:
اطبع("جيد جداً")
غير ذلك اذا درجة >= 50:
اطبع("ناجح")
غير ذلك:
اطبع("راسب")
Output recorded from the compilermeem 04_حل.م · meem 0.17.3+434e13f
جيد جداً

Statements