Control Flow in Raku
Learn conditionals, loops, and branching in Raku — including if/elsif/else, given/when smart matching, ranges, and loop control with practical Docker-ready examples
Control flow determines the order in which your code executes — which branches run, how many times a block repeats, and when to stop. Raku gives you an unusually rich toolkit here, staying true to its TIMTOWTDI (“There Is More Than One Way To Do It”) philosophy.
As a multi-paradigm language, Raku offers familiar structured constructs (if, while, for) alongside expressive features that other languages lack: postfix statement modifiers, the given/when topicalizer built on smart matching, and unless/until as readable negations. Conditions also rely on Raku’s notion of truthiness — 0, empty strings, empty collections, and type objects like Nil are all falsy.
In this tutorial you’ll learn how to branch with if/elsif/else and unless, how to write switch-style logic with given/when, how to iterate using for, while, until, and the bare loop, and how to steer iteration with last, next, and loop labels.
Conditionals: if, elsif, else, unless
Raku’s if needs no parentheses around the condition. It pairs with elsif and else, and adds unless for the negated case. Raku also has a ternary operator written ?? !!, and postfix conditionals that let you attach a condition to the end of a single statement.
Create a file named conditionals.raku:
| |
The postfix form (say "..." if condition) reads almost like English and is idiomatic Raku for short, guarded statements.
Switch with given/when
Raku’s switch statement is given/when. The given block sets the topic variable $_, and each when performs a smart match (~~) against it. Smart matching is what makes when so powerful: it works with ranges, types, regexes, and more — not just equality.
Create a file named given_when.raku:
| |
Each when block automatically succeeds (breaks out) after matching, so there’s no C-style fall-through and no break keyword to remember.
Loops: for, while, until, and loop
Raku’s for iterates over any list or range. You can name the loop variable with the pointy-block arrow ->, or use the implicit topic $_. while and until are condition-driven, and the bare loop runs forever until you break out.
Create a file named loops.raku:
| |
Note <apple banana cherry> is the angle-bracket word-list quoting — a quick way to build a list of strings without commas or quotes.
Loop Control: last, next, and labels
Inside any loop you can use last to exit (like break), next to skip to the next iteration (like continue), and redo to repeat the current iteration. For nested loops, labels let you control an outer loop from inside an inner one.
Create a file named loop_control.raku:
| |
The label OUTER: names the outer for loop, so next OUTER jumps to the outer loop’s next iteration rather than the inner one’s.
Running with Docker
| |
Expected Output
Running conditionals.raku:
It's pleasant
Please log in
Even number
Not zero
adult
Running given_when.raku:
B
It's a string
Looks like a date
Running loops.raku:
12345
Fruit: apple
Fruit: banana
Fruit: cherry
0 => a
1 => b
2 => c
Countdown: 3
Countdown: 2
Countdown: 1
Up: 0
Up: 1
Up: 2
Sum reached 30
Running loop_control.raku:
Counting: 1
Counting: 2
Counting: 3
Odd: 1
Odd: 3
Odd: 5
row 1, col 1
row 2, col 1
row 3, col 1
Key Concepts
- No parentheses needed —
if,while, andfortake their conditions directly; braces define the block. - Postfix modifiers —
say "hi" if $condandsay "hi" unless $condmake short, guarded statements read like English. unlessanduntil— readable negations ofifandwhile, avoiding clumsyif !conditionlogic.- Truthiness —
0,"", empty collections, and type objects likeNilare falsy; most other values are true. given/whenuses smart matching —whenmatches against ranges, types, and regexes, not just equality, and breaks automatically with no fall-through.- The topic variable
$_— set implicitly bygivenand bareforblocks, enabling concise code. - Ranges drive iteration —
1..5and word lists like<a b c>are first-class iterables forforloops. - Loop control with labels —
last,next, andredosteer iteration, and named labels let inner loops control outer ones.
Running Today
All examples can be run using Docker:
docker pull rakudo-star:alpine
Comments
Loading comments...
Leave a Comment