5. Loops
To repeat with لكل (for), مدى (range) and طالما (while), and to stop a loop with توقف (break).
Explanation
Section titled “Explanation”- A loop repeats a block of lines.
لكل ع في مدى(1, 11):goes through the numbers from 1 up to, but not including, 11.طالما شرط:repeats as long as the condition is true.توقف(break) leaves the loop, andتجاوز(continue) moves to the next round.مج += عis short forمج = مج + ع.
Example
Section titled “Example”مج = 0لكل ع في مدى(1, 11): مج += عاطبع(مج)ن = 1طالما ن < 100: ن *= 2اطبع(ن)Common mistakes
Section titled “Common mistakes”مدى(1, 10) does not include 10: its last number is 9.
Exercise
Section titled “Exercise”Print the sum of the even numbers from 1 to 20, then the 3 times table from 1 to 5.
Show the solution
مجموع = 0لكل ع في مدى(1, 21): اذا ع % 2 == 0: مجموع += عاطبع("مجموع الزوجية من 1 إلى 20: ", مجموع)لكل ع في مدى(1, 6): اطبع("3 × ", ع, " = ", 3 * ع)