While Loop
Repeat until a condition is false.
Definition
While loop is one of the most common programming concept. It goes by repeating a process until some condition becomes false. In other words, it repeats the process "while" the condition is true.
Syntax
The syntax of a while loop is very basic and it goes as the following:
- The keyword
طالما
- An opening parentheses
(
- A condition
- A closing parentheses
)
- An opening curly brace
{
- Some code to be executed repeatedly
- A closing curly brace
}
Example
The following example prints the smallest number that can be divided by 4 and by 6:
متغير رقم عدد = 1؛
طالما(!(عدد % 6 == 0 && عدد % 4 == 0)) {
عدد++؛
}
اكتب("العدد " + عدد + " ينقسم على 6 وعلى 4.")؛
danger
If we forgot to add the line number 3, where we increment عدد
by one, our loop wouldn't have finished its work, and it
would've gone forever to terminate!