Iterations¶
for¶
Description¶
The for
loop allows the repetitive execution of a statement or a block of statements for a predefined number of times.
This loop is most useful to
- iterate through arrays or any other sequences of a fixed/known length
- repeat the execution of a statement for as many times as you decide, beforehand.
You can opt for an early exit from a for
, by using the break
statement.
As soon as the break
statement is executed, the loop stops and the Application continues to the next statements.
You can ignore statements from a for
loop's body, by using the continue
statement.
When the continue
statement is encountered:
- All next statements are ignored (in the loop's context)
- The Application continues to the next loop (if exists)
Syntax¶
A for
statement can be written as:
for(initialization; condition; iteration;){
run_this;
}
//or without parentheses
for initialization; condition; iteration;{
run_this;
}
initialization¶
The initialization section of the for
loop defines a loop parameter and gives it an initial value, like so:
data_type identifier = initial_value;
Warning
Note that the definition of the loop variable in the for
loop means that the variable is accessible only by the loop's context. Other contexts, outside of the loop, cannot access it.
Mamba does not allow the use of an external variable as a for
iterator; only local iterators (thus, defined and initialized in the for
statement) can be used.
Warning
Unlike other programming languages, the initialization part expects one, and only one, statement.
So that the following will throw a Validation exception:
for(int i = 0, y = 1; i < 5; i + 1, y + 2)
condition¶
The condition section is any boolean expression that is evaluated to determine whether the loop should be ran or not. If the condition expression is evaluated as true, the loop's statement will be executed until the next condition is evaluated again. Otherwise, the loop will finish and the Application will proceed to the next statement.
iteration¶
The iteration part defines what should happen every time that a loop finishes its execution.
Warning
Unlike other programming languages, the iteration part expects one, and only one, statement.
So that the following will throw a Validation exception:
for(int i = 0, y = 1; i < 5; i + 1, y + 2)
Example¶
//Simple loop, running for 4 (0-3) times
for int i = 0; i < 3; i + 1 {
DebugLib.Logger.WriteInfoLine(i);
}
/*
Output:
0
1
2
*/
//Loop with a "continue" statement
DebugLib.Logger.WriteInfoLine("The odd numbers in [0, 99] are: ");
for int i = 0; i < 100; i + 1 {
if(i % 2 == 0){ //Do not output i if i is an even number
continue;
}
//The next line will not be executed if the "continue" is executed
DebugLib.Logger.WriteInfoLine(i);
}
/*
Output:
The odd numbers in [0, 99] are:
1
3
5
...
97
99
*/
//Loop with a "continue" and "break" statement
DebugLib.Logger.WriteInfoLine("The odd numbers in [0, 10] are: ");
for int i = 0; i < 100; i + 1 {
//Stop the loop when you reach to 10 (even though it would loop for 100 times)
if(i > 10){
break;
}
//Do not output i if i is an even number. Will be ran only if the "break" never executed
if(i % 2 == 0){
continue;
}
//The next line will not be executed if the "continue" is executed
DebugLib.Logger.WriteInfoLine(i);
}
/*
Output:
The odd numbers in [0, 10] are:
1
3
5
7
9
*/
foreach¶
Description¶
The foreach
loop is used when an iteration through a Collection or an Array is required. It allows a repetitive execution of a statement (or block of statements) for a X number of times, where X is the size of a Collection or an Array.
You can opt for an early exit from a for
, by using the break
statement.
As soon as the break
statement is executed, the loop stops and the Application continues to the next statements.
You can ignore statements from a for
loop's body, by using the continue
statement.
When the continue
statement is encountered:
- All next statements are ignored (in the loop's context)
- The Application continues to the next loop (if exists)
Syntax¶
A foreach
statement can be written as:
foreach (iterator in collection){
run_this;
}
//Alternatively, without parentheses
foreach iterator in collection{
run_this;
}
collection¶
The collection section contains an identifier of an Array, Collection or Dictionary type. Basically, it represents a collection of similar (same type) objects.
iterator¶
The iterator section of the foreach
loop defines the current object in the collection, as it is being traversed.
You can either define it in a strict mode, for example:
foreach int i in collection{} //strongly typed
foreach var item in collection{} //loosely typed, using variable
foreach item in collection{}
**Important: ** The iterator is accessible only by the foreach
loop. Other scopes, external to the loop, cannot access it.
Example¶
Array[int] numbers = {1, 2, 3, 4, 5};
foreach number in numbers { //Notice that we did not define the type of the number
DebugLib.Logger.WriteInfoLine(number);
}
/*
Output:
1
2
3
4
5
*/
Collection[string] strings;
strings.Add("A");
strings.Add("B");
strings.Add("C");
foreach string aString in strings { //More strict iterator definition
DebugLib.Logger.WriteInfoLine(aString);
}
foreach user in Domain.ApplicationUser.GetAll() {
//Do not iterate any further if you found a user with more than 3 failed logins
if(user.AccessFailedCount > 3){
break;
}
//Go to the next user if his E-Mail has been confirmed
if(user.EmailConfirmed){
continue;
}
user.ConfirmEmail();
}
Caution
Be very careful when you alter the collection you are iterating through. This might cause unexpected behavior and application errors.
Example: imagine you have a collection of 10 numbers. While iterating through that, using the foreach
loop, you delete the current item.
The next time that the next item will be fetched, the collection will be changed, having 9 items. That will confuse the processor and might result into Exceptions
while¶
Description¶
The while
statement runs a statement (or a block of statements) until the specified condition is evaluated as false
(or a break
statement is executed).
You can opt for an early exit from a for
, by using the break
statement.
As soon as the break
statement is executed, the loop stops and the Application continues to the next statements.
You can ignore statements from a for
loop's body, by using the continue
statement.
When the continue
statement is encountered:
Syntax¶
A while
statement can be written as:
while (condition){
execute_this;
}
//alternatively, without parentheses
while condition{
execute_this;
}
Example¶
//A never ending loop, running till the end of time
while (true) {
DebugLib.Logger.Write("Never ending loop.");
}
int i = 0;
while(i < 5){
DebugLib.Logger.WriteInfoLine(i);
i = i + 1;
}
/*
Output:
0
1
2
3
4
*/
int i = 0;
while(i < 1000){
//Stop at 10, even though the loop iterates for 1000 times
if(i >= 10){
break;
}
//Do nothing if the number is even
if(i % 2 == 0){
i = i + 1;
continue;
}
DebugLib.Logger.WriteInfoLine(i);
i = i + 1;
}
/*
Output:
1
3
5
7
9
*/