Easy Tutorial
❮ Perl Next Statement Perl Nested Loops ❯

Perl UNLESS...ELSIF Statement

Perl Conditional Statements

An unless statement can be followed by an optional elsif statement, and then another else statement.

This type of conditional statement is very useful in cases with multiple conditions.

When using unless, elsif, and else statements, you need to keep the following points in mind:

Syntax

The syntax is as follows:

unless(boolean_expression 1){
   # Execute when boolean_expression 1 is false
}
elsif(boolean_expression 2){
   # Execute when boolean_expression 2 is true
}
elsif(boolean_expression 3){
   # Execute when boolean_expression 3 is true
}
else{
   # Execute when no condition is matched
}

Example

#!/usr/bin/perl

$a = 20;
# Using unless statement to check the boolean expression
unless( $a == 30 ){
    # Execute when the boolean expression is false
    printf "a is not 30\n";
}elsif( $a == 30 ){
    # Execute when the boolean expression is true
    printf "a is 30\n";
}else{
    # Execute when no condition is matched
    printf "a is $a\n";
}

Executing the above program will output:

a is not 30

Perl Conditional Statements

❮ Perl Next Statement Perl Nested Loops ❯