Lesson 5 of 15

If / Unless

Conditional Statements

if / elsif / else

my $x = 10;
if ($x > 0) {
    say "positive";
} elsif ($x == 0) {
    say "zero";
} else {
    say "negative";
}

Comparison Operators

Perl has separate operators for numbers and strings:

NumericStringMeaning
==eqEqual
!=neNot equal
<ltLess than
>gtGreater than
<=leLess or equal
>=geGreater or equal

unless

unless is the opposite of if -- it executes when the condition is false:

my $age = 20;
unless ($age < 18) {
    say "adult";
}

Your Task

Write an if/elsif/else that classifies a number: print positive, zero, or negative.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.