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:
x
takes 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:
u
iterates over eachuser
entity that matches the condition@* {}
, which selects all users without any filtering criteria.- During each iteration, the
print(u.name)
statement prints thename
attribute of the currentuser
entity.
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
for
loop iterates over alluser
entities in the blockchain (specified byuser @* {}
). - During each iteration, it checks if the
company
attribute of the currentuser
entity is equal to'Facebook'
. - If the condition is met, the
print(u.name)
statement prints thename
attribute of the current user and then thebreak
statement is executed. - The
break
statement immediately terminates the loop, meaning that no more iterations will occur even if there are moreuser
entities 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
while
loop continues to execute as long as the conditionx < 5
is true. - Within the loop, there's a conditional statement that checks if the value at index
x
in thevalues
array is equal to3
. - If the condition is met, the
break
statement 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
x
by 1. - This loop iterates through the
values
array, 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
for
loop iterates over alluser
entities (specified byuser @* {}
). - During each iteration, it checks if the
company
attribute of the currentuser
entity is equal to'BigCompanyCo'
. - If the condition is met (if the user works at 'BigCompanyCo'), the
continue
statement is executed. - The
continue
statement 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 thename
attribute of the current user.