Course of Raku / Essentials / Conditional checks

if blocks

The if conditional check tests a condition and if it is True, runs a block of code.

if 10 > 4 {
    say 'Mathematics works!';
}

Notice that you don’t need to put the condition in parentheses (but you can if you wish).

Of course, variables are more than welcome in the tests:

my $flag = False;
if $flag {
    # . . . do something
}

In the case when more than one check uses the same variable, it is possible to use chained comparisons:

my $x = 42;
if 40 < $x < 45 {
    say "Correct answer $x is given.";
}

If the condition is not satisfied, the associated block of code is not executed, and the program flow continues.

say 'Begin';
if False {
    say 'This is never printed.';
}
say 'End';

This program just prints Begin and End.;

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Conditional checks   |   Conditional checks / else blocks


💪 Or jump directly to the exercises to this section.