flang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.
Fuzion LanguageTutorialLoops
🡄 🡅 🡆

Loops

Simple counting loops

Let's start with a simple loop that counts to 10.







Now let's refine this example to better understand Fuzion's powerful loop statement. Many loops have an index field that is initialized during the first loop iteration and updated after each iteration. Index fields are defined in a for clause that can give an initial value and, after a comma, an expression to calculate the value for the next iteration. In our loop example, we define i as an index field as follows:







Oops, this code does not compile: We forgot to remove the code that updates the index field i. In Fuzion, index fields are not modifiable outside of the for-clause of the loop. The corrected example:







Not modifying the index field has an enormous advantage: The field is an immutable field within the loop body, which allows for compiler optimizations and for cheap accesses within closures, even accesses from other threads are automatically safe.

Endless loop

The loop termination condition that so far was given in the while part can be omitted to create an endless loop, as shown in the next example:







An endless loop is typically not very useful, the main exception being the control loop in a task that is supposed to continue forever.

However, as we will see next, iterating loops that operate over a finite stream of elements do not need an explicit termination condition, which will be shown in the following examples.

Iterating loops

Using a simple iterating loop enables us to further simplify the code of the previously presented counting loop by iterating over an interval:







What happens here is logically different from the previous loops: The expression 1..10 calls the feature infix .. on the integer constant 1 with the argument 10. This feature defines an interval from the value it is called on to the value passed as an argument. So, in this case, this defines an interval from 1 to 10, inclusive.

An interval is streamable, which means that that it can be used to define a loop index field using in.

What this means is that the previous example uses syntactic sugar that the compiler expands to something like the following







This looks as if the code could be significantly slower to run than the simple index field in the earlier examples. However, the compiler's optimization will result in code that is almost identical.

Steps

The ability to loop over arbitrary streams of indices enables as well to loop over integers using a step size larger than 1. Here is an example that prints only even numbers by using a step of 2:







This loop is essentially the same as the previous loop, only that the interval that the loop iterates over now was created using a step size, which is done using the operator infix : on an integer interval.


next: Advanced Loops


🡄 🡅 🡆