What Are Loops?


Loops are used to repeat the same code several times. So if we want to execute a code more than once we write it inside a loop which execute it as we want based on a conditions we specify.
When the loop executed, the statements inside it is executed separately from other statements in the program, which means you could consider that all statements inside the loop are in a special place in the memory, this place is called "scope". After all loop statements in this scope executed, the scope is erased completely from the memory.

for Loop


For loop is used mostly if the number of repetitions is known. It has the following form in Alusus:


for initialization, condition, update {
  statements
}

Initialization step is done first and only once. This step allows to initialize variables that control the loop.
After, the condition is evaluated and if it is True, then the loop body will be executed, and if it is False the loop body won't be executed (this includes all statements inside the curly brackets, which means the scope) and the workflow will jump to the first statement after the loop.
After executing the loop body the workflow moves to the update statement, which allows us to update the variables that control the loop.
After that the condition is evaluated again, if it is true the loop body will be executed and the same operation is repeated until the condition is false.

Example:

import "Srl/Console.alusus";
use Srl.Console;
def i:int;
for i = 2, i < 11, i = i+1 {
  print("value of i: %d\n", i);
}
/*
value of i: 2
value of i: 3
value of i: 4
value of i: 5
value of i: 6
value of i: 7
value of i: 8
value of i: 9
value of i: 10
*/

while Loop


It could be used in any case, but mostly we use while loop when the number of repetitions of the code (or, more precisely, the scope) is unknown. It has the following form:

while condition {
  statements
}

Here we do the initialization step to the control element before executing the loop, and its body must contain a statement that updates the condition variables, otherwise we will enter an infinite loop.

Example:
The same previous code (printing numbers from 2 to 10) but with while loop.

import "Srl/Console.alusus";
use Srl.Console;
def i:int=2; // we must initialize the control element before the loop
while(i<11) {
  print("value of i: %d\n", i);
  i=i+1; // also we must update the control element value to prevent infinite loop
}

Loops Control Commands: break And continue


They are commands to control the the loops workflow

break
As soon as `break` command is executed, it will stop the whole execution of the loop scope and exit it and clear it from the memory, then move to the statement that follows the loop in the program. It could be used with all kinds of loops.
For example, in the previous code if we want to stop the execution of the loop (exit the loop) when we reach number 7, we could use this command as follows:

import "Srl/Console.alusus";
use Srl.Console;
def i:int;
for i = 2, i < 11, i = i+1 {
  if i==7  break;
  print("value of i: %d\n", i);
}

/*
value of i: 2
value of i: 3
value of i: 4
value of i: 5
value of i: 6
*/

Note that the rest of numbers are not printed because when we reach to number 7 the condition became true and `break` command is executed.

continue
It is used to skip a specific case (a repetition or more) in the loop, which means we use it to skip some part of the scope code. For example, in the previous code if we don't want to print number 7 we could write the following:

import "Srl/Console.alusus";
use Srl.Console;
def i:int;
for i = 2, i < 11, i = i+1 {
  if i==7  continue;
  print("value of i: %d\n", i);
}

/*
value of i: 2
value of i: 3
value of i: 4
value of i: 5
value of i: 6
value of i: 8
value of i: 9
value of i: 10
*/

Note that number 7 is not printed because when we reach it the condition became true and `continue` command is executed, which made the compiler ignore the next statements in the scope and return to the start of the loop scope.

Note: `break` and `continue` commands in Alusus allow the user to specify the number of loops to exit. For example if you have two nested loops and you want to exit both of them when a given condition is true, it will be easier to use this idea. As in the next example.
Here we will exit the inner loop only when the condition "i==4" is true, hence 0 and 1 won't be printed.

import "Srl/Console.alusus";
use Srl.Console;
def i:int;
def j:int;
for i = 0, i < 6, ++i {
  for j = 0, j < 2, ++j {
    if i == 4 {
      print("Skip it\n")
      break;
    }
    print("%d\n",j);
  }
  print("**************\n")
}

/*
0
1
**************
0
1
**************
0
1
**************
0
1
**************
Skip it
**************
0
1
**************
*/

Now we will edit the previous example so that we exit both loops when the condition is true.

import "Srl/Console.alusus";
use Srl.Console;
def i:int;
def j:int;
for i = 0, i < 6, ++i {
  for j = 0, j < 2, ++j {
    // This time we will exit both loops
    if i == 4 {
      print("Skip it\n")
      break 2; // we specified the number of loops to exist as 2
    }
    print("%d\n",j);
  }
  print("**************\n")
}

/*
0
1
**************
0
1
**************
0
1
**************
0
1
**************
Skip it
*/

The same idea holds for `continue` command, but here we will skip the next statements in the inner loop scope and enter a new repetition when the condition "j==1" is true.

import "Srl/Console.alusus";
use Srl.Console;
def i:int;
def j:int;
for i = 0, i < 6, ++i{
  for j = 0, j < 3, ++j{
    if j == 1 {
      print("Skip it\n")
      continue;
    }
    print("%d\n",j);
  }
  print("**************\n")
}

/*
0
Skip it
2
**************
0
Skip it
2
**************
0
Skip it
2
**************
0
Skip it
2
**************
0
Skip it
2
**************
0
Skip it
2
**************
*/

Now we will edit the previous code so that `continue` command will be used to move to the next repetition of the outer loop.

import "Srl/Console.alusus";
use Srl.Console;
use Srl;
def i:int;
def j:int;
for i = 0, i < 6, ++i{
  for j = 0, j < 3, ++j{
    if j==1 {
      print("Skip it\n")
      continue 2;
    }
    print("%d\n",j);
  }
  print("**************\n")
}

/*
0
Skip it
0
Skip it
0
Skip it
0
Skip it
0
Skip it
0
Skip it
*/