Updated March 29, 2023
Introduction to Perl else if
The “if-else” statement is useful for choosing one condition but when comes to multiple conditions then the “Perl else if” statement is useful in the technology. It is useful for the same priority multiple conditions come in the statement and select only one condition in this technology. It works from top to bottom and passes the argument one by one in the else if statement. “else if” is technically called Perl “elsif” and used as a statement in the llanguage.
Syntax
else if (elsif) statement syntax is below:
elsif(condition for statement)
{
Perl code for statement;
}
else if (elsif) statement used in condition syntax is below:
if(condition_1 for statement)
{
Perl code for statement execution (if the condition is true);
}
elsif(condition_2 for statement)
{
Perl code for statement execution (if the condition is true);
}
elsif(condition_3 for statement)
{
Perl code for statement execution (if the condition is true);
}
else(condition_4 for statement)
{
Perl code for statement execution (if all above conditions are failed);
}
The Perl else if (elsif) rules for working syntax is below:
- It is placed after “if statement” and before “else statement”.
- It needs more than two conditions in the code.
- It succeeds when “if condition” became failed.
- It succeeds then the remaining elsif and else condition will be failed.
How else if Statement Work in Perl?
Download and install the latest version of your operating system of the device.
https://www.Perl.org/ or http://strawberryPerl.com/ are mostly using the Perl IDE website link.
Create a file with the .pl extension and save the file in the required command line path.
Example:
helloo.pl or first pearl.pl
Use else if (elsif) statement syntax in the code.
if(condition_1 for statement)
{
Perl code for statement execution (if the condition is true);
}
elsif(condition_2 for statement)
{
Perl code for statement execution (if the condition is true);
}
elsif(condition_3 for statement)
{
Perl code for statement execution (if the condition is true);
}
else(condition_4 for statement)
{
Perl code for statement execution (if all above conditions are failed);
}
Description of Perl else if statement:
- Firstly if statement is passed as an argument when if the condition becomes failed then else if (elsif) statement passed as an argument.
- Condition become failed then another else if (elsif) statement passed as an argument.
- When last else if (elsif) condition becomes failed then else condition become true.
- The else condition becomes true then if statement and available all else if statements are failed.
- It is used with the “Perl unless” statement for multiple condition statements.
The working sample of the else if the statement is below:
Code:
$number_var = 240;
if($number_var == 40)
{
print "the given number is 40 \n";
}
elsif($number_var == 24)
{
print "the given number is 24 \n";
}
elsif($number_var== 240)
{
print "the given number is 240 \n";
}
else
{
print "the given number is not present here... \n";
}
Examples of Perl else if
Given below are the examples:
Example #1
The Simple else if (elsif) statement Example and Output.
Code:
$number_var = 240;
if($number_var == 40)
{
print "the given number is 40 \n";
}
elsif($number_var == 24)
{
print "the given number is 24 \n";
}
elsif($number_var== 240)
{
print "the given number is 240 \n";
}
else
{
print "the given number is not present here... \n";
}
Output:
Example #2
Statement with If statement true Example and Output.
Code:
$number_var = 410;
if($number_var == 410)
{
print "the given number is 410 \n";
}
elsif($number_var == 240)
{
print "the given number is 240 \n";
}
elsif($number_var== 420)
{
print "the given number is 420 \n";
}
else
{
print "the given number is not present here... \n";
}
Output:
Explanation:
- The if Statement has the true condition.
- The above example displays the if statement output on the screen.
Example #3
Statement with else statements Example and Output.
Code:
$number_var = 840;
if($number_var < 410)
{
print "the given number is 410 \n";
}
elsif($number_var > 940)
{
print "the given number is 240 \n";
}
elsif($number_var == 420)
{
print "the given number is 420 \n";
}
else
{
print "the given number is not present here... \n";
}
Output:
Explanation:
- The if Statement and else If (elsif) statement has the wrong condition.
- The above example displays the else statement output on the screen.
Example #4
Statement with unless statement Example and Output.
Code:
$number_var = 840;
unless($number_var == 840)
{
print "the given number is not 410 \n";
}
elsif($number_var == 840)
{
print "the given number is 840 \n";
}
else
{
print "the given number is not present here... \n ";
}
Output:
Explanation:
- The above example unless and else if statements are the same.
- Statement passed as an argument first then Unless Statement.
- It is displayed as an output on the screen.
Example #5
Statement with multiple else if Example and Output.
Code:
$number_var = 840;
if($number_var < 240)
{
print "the given number is not 410 \n";
}
elsif($number_var > 240)
{
print "the given number is greater than 240 \n";
}
elsif($number_var == 840)
{
print "the given number is 840 \n";
}
else
{
print "the given number is not present here... \n ";
}
Output:
Explanation:
- More than two else if (elsif) statements are true then first else if statement display as output.
- Above example both “else if” statement is true but the first statement is displayed as output.
Conclusion
The else if (elsif) statement makes it easy and simple to choose multiple conditions in this technology. The statement useful for similar priorities conditions and their one output.
Recommended Articles
This is a guide to Perl else if. Here we discuss the introduction, how else if statement works in perl? along with examples respectively. You may also have a look at the following articles to learn more –