4. Conditions
To choose between two paths with اذا (if) and غير ذلك (else).
Explanation
Section titled “Explanation”- 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
Section titled “Example”درجة = 75اذا درجة >= 90: اطبع("ممتاز")غير ذلك اذا درجة >= 60: اطبع("ناجح")غير ذلك: اطبع("راسب")Common mistakes
Section titled “Common mistakes”Forgetting the ::
س = 3اذا س > 2 اطبع(س)Exit code: 2
In the error catalog: E0019
س = 3اذا س > 2: اطبع(س)Or uneven indentation:
اذا صواب: اطبع(1) اطبع(2)Exit code: 2
In the error catalog: E0006
اذا صواب: اطبع(1) اطبع(2)Exercise
Section titled “Exercise”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
درجة = 85اذا درجة >= 90: اطبع("ممتاز")غير ذلك اذا درجة >= 75: اطبع("جيد جداً")غير ذلك اذا درجة >= 50: اطبع("ناجح")غير ذلك: اطبع("راسب")