# Loop statements · Chromia Docs (concept)

> In Rell, loop statements include the for, while, break, and continue statements, each serving distinct purposes for controlling program flow.

Source: https://docs.chromia.com/rell/language-features/statements/loop-statements/

# Loop statements

On this page

* [The for loop](#the-for-loop)
* [The while loop](#the-while-loop)
* [The break statement](#the-break-statement)
* [The continue statement](#the-continue-statement)

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:

Rell

```
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 of
  [`range(10)`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/range/), which generates a
  sequence of numbers from 0 to 9.
* During each iteration, the
  [`print(x)`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/print/) statement prints the
  current value of `x`.

In the second `for` loop:

* `u` iterates over each `user` entity that matches the condition `@* {}`, which selects all users without any filtering
  criteria.
* During each iteration, the `print(u.name)` statement prints the `name` attribute of the current `user` entity.

The expression after `in` may return a `range` or a collection
([`list`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/list/),
[`set`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/set/),
[`map`](https://docs.chromia.com/pages/rell-stdlib/-rell-system-library/root/map/)).

Can use tuple unpacking in a loop:

Rell

```
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:

Rell

```
var x = 0;
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:

Rell

```
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 all `user` entities in the blockchain (specified by `user @* {}`).
* During each iteration, it checks if the `company` attribute of the current `user` entity is equal to `'Facebook'`.
* If the condition is met, the `print(u.name)` statement prints the `name` attribute of the current user and then the
  `break` statement is executed.
* The `break` statement immediately terminates the loop, meaning that no more iterations will occur even if there are
  more `user` 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 condition `x < 5` is true.
* Within the loop, there's a conditional statement that checks if the value at index `x` in the `values` array is equal
  to `3`.
* 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 value `3`. 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:

Rell

```
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 all `user` entities (specified by `user @* {}`).
* During each iteration, it checks if the `company` attribute of the current `user` 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 the `name` attribute of the current user.

[View this page on docs.chromia.com](https://docs.chromia.com/rell/language-features/statements/loop-statements/)Content mirrored from the live docs
