Loop statements
In Rell, loop statements include the for, while, break, and continue statements, each serving distinct purposes
for controlling program flow. This summary provides an overview of these loop statements and their applications.
The for loop
The for loop iterates over a specified range or collection, executing a block of code for each element. For example:
for (x in range(10)) {
print(x);
}
for (u in user @* {}) {
print(u.name);
}
In the first for loop:
xtakes on values from 0 to 9 (inclusive) because ofrange(10), which generates a sequence of numbers from 0 to 9.- During each iteration, the
print(x)statement prints the current value ofx.
In the second for loop:
uiterates over eachuserentity that matches the condition@* {}, which selects all users without any filtering criteria.- During each iteration, the
print(u.name)statement prints thenameattribute of the currentuserentity.
The expression after in may return a range or a collection (list, set, map).
Can use tuple unpacking in a loop:
val l: list<(integer, text)> = get_list();
for ((n, s) in l) { ... }
The while loop
The while loop repeatedly executes a block of code as long as a specified condition remains true. For example:
while (x < 10) {
print(x);
x = x + 1;
}
This loop prints values of x while x is less than 10.
The break statement
The break statement is used to prematurely exit a loop when a certain condition is met. For example:
for (u in user @* {}) {
if (u.company == 'Facebook') {
print(u.name);
break;
}
}
while (x < 5) {
if (values[x] == 3) break;
x = x + 1;
}
In the first for code snippet,
- The
forloop iterates over alluserentities in the blockchain (specified byuser @* {}). - During each iteration, it checks if the
companyattribute of the currentuserentity is equal to'Facebook'. - If the condition is met, the
print(u.name)statement prints thenameattribute of the current user and then thebreakstatement is executed. - The
breakstatement immediately terminates the loop, meaning that no more iterations will occur even if there are moreuserentities to process. - This loop searches for a user whose company is
'Facebook', and as soon as such a user is found and printed, the loop stops.
In the second while code snippet,
- The
whileloop continues to execute as long as the conditionx < 5is true. - Within the loop, there's a conditional statement that checks if the value at index
xin thevaluesarray is equal to3. - If the condition is met, the
breakstatement is executed, causing the loop to immediately terminate. - If the condition is not met, the loop continues to the next iteration, incrementing the value of
xby 1. - This loop iterates through the
valuesarray, looking for the value3. As soon as it finds such a value, the loop stops executing.
In both cases, the break statement is used to exit a loop prematurely based on a specific condition. It provides a way
to control the flow of the loop and terminate it early when certain criteria are met.
The continue statement
The continue statement is used to skip the remaining iterations of the current loop iteration and proceed to the next
iteration. For example:
for (u in user @* {}) {
if (u.company == 'BigCompanyCo') {
continue;
}
print(u.name); // Will print every user who does not work at BigCompanyCo.
}
In this example:
- The
forloop iterates over alluserentities (specified byuser @* {}). - During each iteration, it checks if the
companyattribute of the currentuserentity is equal to'BigCompanyCo'. - If the condition is met (if the user works at 'BigCompanyCo'), the
continuestatement is executed. - The
continuestatement causes the loop to skip the rest of the code inside the loop block and move on to the next iteration immediately. - If the condition is not met (if the user does not work at 'BigCompanyCo'), the
print(u.name)statement is executed, printing thenameattribute of the current user.