Course of Raku / Essentials / Loops

while and until as statement modifiers

Both while and until can be used in the form of statement modifiers (similarly to if and unless).

Consider an example that emulates modulo division:

my $x = 10;
$x -= 3 while $x > 2;
say $x; # 1

Here, -= is the operator that combines both subtraction and assignment. In this case, $x -= 3 is equivalent to $x = $x - 3.

The while modifier lets the statement $x -= 3 to repeat while the condition $x > 2 remains True. As soon as it becomes False, the loop stops.

Notice that if the condition is False initially, the statement is not executed at all.

The same program can be rewritten with until. For this, the condition must be inverted:

my $x = 10;
$x -= 3 until $x <= 2;
say $x; # 1

Course navigation

Loops / Using repeat   |   Loops / Three-statement loop


💪 Or jump directly to the exercises to this section.