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:
| Numeric | String | Meaning |
|---|---|---|
== | eq | Equal |
!= | ne | Not equal |
< | lt | Less than |
> | gt | Greater than |
<= | le | Less or equal |
>= | ge | Greater 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.