Else if

Updated: 06/30/2019 by Computer Hope

Alternatively called elsif, else if is a conditional statement performed after an if statement that, if true, performs a function. Below is an example of an if, elsif, and else conditional statement in Perl.

#!/usr/bin/perl
print "Enter number: ";
my $number = <STDIN>;
if ($number <= 10) {
 print "Your number is less than or equal to 10";
}
elsif ($number <= 50) {
 print "Your number is more than 10, but less than 50";
}
else {
 print "Your number is greater than 50";
}

The above example shows how elsif could be used to create an additional conditional statement and not only an if or else statement. The above example could also be extended by adding as many elsif or else if statements as the program needed.

Note

Not all programming languages are the same. Although the example above used elsif, in a different language, such as ANSI C (American National Standards Institute) or Python, you would use else if or elif respectively.

Conditional statement, Else, If statement, Programming terms