Course of Raku / Essentials / Control flow essentials / Conditional checks
Using unless
All Boolean conditions can be easily negated with the !
operator. However, in some cases, an explicit negation makes the
expression heavier and less readable. In these cases,
unless can be a friend.
The unless block is executed when its condition is
False.
my $broken = False;
# ...Something can set $broken to True here...
unless $broken {
say "Don’t worry!";
say "Be happy!";
}Compare the two variants of the same program:
With if and negation:
if !$broken { . . . }With unless:
unless $broken { . . . }Having the alternatives, you can always choose what feels better to you in the current situation.
Notice that unless cannot be followed by an
else or elsif block. In this case, the only
way is to use if.
Course navigation
← Quiz:
if, elsif, and else | if and
unless as statement modifiers →
💪 Or jump directly to the exercises in this
section.
Translations of this page: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська