if elsif else statement in perl

In Perl, the if-elsif-else statement allows you to test multiple conditions and execute different blocks of code based on the first true condition. The basic syntax of the if-elsif-else statement is:

refer to‮itfigi:‬dea.com
if (condition1) {
    # Code to execute if condition1 is true
} elsif (condition2) {
    # Code to execute if condition2 is true
} else {
    # Code to execute if all conditions are false
}

Here, condition1, condition2, and so on, are Boolean expressions that are evaluated in the order they appear. The first condition that is true causes the corresponding code block to be executed. If all conditions are false, the code block enclosed in the else block is executed.

For example, the following code checks whether a given number is positive, negative, or zero using the if-elsif-else statement:

my $num = -10;

if ($num > 0) {
    print "$num is positive\n";
} elsif ($num < 0) {
    print "$num is negative\n";
} else {
    print "$num is zero\n";
}

In this code, the first condition $num > 0 checks whether the number is greater than zero, which indicates that the number is positive. If the condition is true, the code block print "$num is positive\n"; is executed, which prints a message indicating that the number is positive. If the first condition is false, the second condition $num < 0 checks whether the number is less than zero, which indicates that the number is negative. If the second condition is true, the code block print "$num is negative\n"; is executed, which prints a message indicating that the number is negative. If both conditions are false, the else block is executed, which prints a message indicating that the number is zero.