Using loops
Loop constructions help organize repeated actions. There are a few different options in Perl 6 for creating a loop. Let's start with the one that is similar to traditional loops in C style.
The loop cycle
The loop
keyword expects three elements to control the number or repetitions of the loop body. Consider the following code snippet:
loop (my $c = 0; $c < 5; $c++) { say $c; }
In this example, $c
is the counter of the loop iterations. This variable is declared and initialized immediately after the loop keyword—my $c = 0
. The body of the loop is executed if the condition $c < 5
is True
. After the iteration, the $c++
statement is executed, which increments the counter, and the cycle repeats. As soon as $c
becomes equal to five, the condition is not True
anymore, and the loop stops. So, the whole program prints the numbers from 0
up to, and including, 4
.
Some, or even all of the parts in the loop header may be omitted. For example, if the counter variable is initialized before...