Course of Raku / Essentials / Loops

Postfix form of for

We have already seen other statement modifiers: if and else and while and until. And that’s the reason for a small celebration as the design of Raku is very consistent.

With a short loop body, the for loop can be written in a postfix form:

.say for 1..7;

As you see, ranges are very natural when used with the postfix form of for.

This program is equivalent to a more traditional variant:

for 1..7 -> $n {
    say $n;
}

Or, if the default loop variable is used, to:

for 1..7 {
    .say;
}

Here, .say is the short form for $_.say.

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Loops / Topic variable   |   💪 Exercises: Loops

Translations of this page: EnglishDeutschEspañolItalianoLatviešuNederlandsБългарскиРусскийУкраїнська