For Loop
Repeat for specific number of times.
Definition
For loops are a special kind of while loops. They execute some process repeatedly with respect to a change in some counter. Everything that is done using for-loops can be done by while-loops as well.
Syntax
The syntax for for-loops goes as the following:
- The keyword
كرر
- An open parenthesis
(
- Declaring a counter variable or assigning a value to a declared variable
- A semicolon
؛
- The The upper\lower limit of the counter
- A semicolon
؛
- The value change that should happen on the counter after each iteration (can be a postfix, prefix, or a binary assignment operation)
- A closing parenthesis
)
- An opening curly brace
{
- Some code that is to be executed repeatedly
- A closing curly brace
{
Examples
Example 1:
The following example prints the numbers from 0 to 9:
- We are declaring the counter
س
as a number variable with value 0 - We configured our loop to execute while the value of
س
is less than 10 - We configured our loop to increment
س
by 1 after each iteration - In the body, we are printing the value of
س
كرر (متغير رقم س = 0؛ س < 10؛ س++) {
أكتب(س)؛
}
Example 2:
متغير رقم س؛
كرر (س = 0؛ س <= 10؛ س += 2) {
أكتب(س)؛
}