Skip to content
العربية

5. Loops

To repeat with لكل (for), مدى (range) and طالما (while), and to stop a loop with توقف (break).

  • 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
مج = 0
لكل ع في مدى(1, 11):
مج += ع
اطبع(مج)
ن = 1
طالما ن < 100:
ن *= 2
اطبع(ن)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
55128

Try this example in the playground

مدى(1, 10) does not include 10: its last number is 9.

Print the sum of the even numbers from 1 to 20, then the 3 times table from 1 to 5.

Show the solution
Solution
مجموع = 0
لكل ع في مدى(1, 21):
اذا ع % 2 == 0:
مجموع += ع
اطبع("مجموع الزوجية من 1 إلى 20: ", مجموع)
لكل ع في مدى(1, 6):
اطبع("3 × ", ع, " = ", 3 * ع)
Output recorded from the compilermeem 05_حل.م · meem 0.17.3+434e13f
مجموع الزوجية من 1 إلى 20: 1103 × 1 = 33 × 2 = 63 × 3 = 93 × 4 = 123 × 5 = 15

Statements · The builtin مدى