Updated April 1, 2023
Introduction to Perl if statements
In Perl, if statements are the conditional statements that work on either or statements, i.e. it executes the program or task based on the condition that can be true or false. If statements come in three forms single if statement that evaluates only true condition, if else statement that evaluates both true and false condition and if else if that evaluates condition using a nested structure where various conditions have been evacuated to test the single value.
Types of if statements in Perl
Given below are the types:
1. if statement
if statement is used to evaluate a true condition.
Syntax:
if(condition)
{
statement to be executed;
}
Here if is a keyword used for the if statement, if the condition is true then it will transfer the flow control inside the loop and will execute the if block, if the given condition is false, then it will not execute the if block and terminate the program.
Example to implement if statement in Perl
let us see an example
Code:
print "Enter a Number \n";
$number = <>;
if( $number >= 0 )
{
printf "You have entered Positive number! \n";
}
Output:
Explanation:
- Here we have written a program to check a positive number, the print will prompt a message for the user to Enter a Number, then the user will enter the value, the value entered by the user will be stored in number variable.
- $number >= 0 condition is used to check positive number if the value entered by the user is greater than or equal to 0, it will execute the if block and print You have entered Positive number if the entered value is negation i.e less than 0. it will end the program.
2. if else statement
if else statement is used to evaluate both true and false conditions.
Syntax:
if(condition)
{
statements to be executed;
}
else
{
statements to be executed;
}
Here if and else are the keywords used for of else statement. If the given condition is true the flow control will execute the if block i.e. statements inside if block, if the given condition is false then the flow control will execute the else blocks i.e. statements inside the else block.
Example to implement if else in Perl
Let us see with the help of an example
Code:
print "Enter a Number \n";
$number = <>;
if( $number >= 0 )
{
printf "You have entered Positive number! \n";
}
else
{
printf "You have entered Negative number! \n";
}
Output:
Explanation:
- Here we have written a program to check positive or negative number, print statement will prompt the message enabling a user to enter a number, the number variable is used to store the number entered by the user.
- If the number entered by the user is greater than or equal to 0, then it will execute the if block and print You have entered Positive number.
- If the entered number is less than 0, then it will execute else block and print You have entered a Negative number.
3. if else if statement
if else if statement is used to evaluate multiple conditions for a single value.
Syntax:
if(condition)
{
statement to be executed;
}
elsif(condition 2)
{
statement to be executed;
}
elsif(condition 3)
{
statement to be executed;
}
....
....
else
{
statement to be executed;
}
if elsif and else are the keywords used for if else if statement. If the first condition is true then it will execute the if block else, it will transfer the flow control elsif block. If the condition mentioned in elsif is true then it will execute that block else transfer the control to another elsif statement. Like this, the procedure will continue for all blocks until the condition becomes true. If all the condition is false, then it will transfer control to else statement and execute the statement inside the else statement.
Example to implement if else if statement in Perl
Let us understand better with the help of an example
Code:
print "Enter the Percentage \n";
$percentage = <>;
if( $percentage < 0 || $percentage > 100)
{
printf "Enter valid percentage! \n";
}
elsif( $percentage > 0 && $percentage < 35)
{
printf "Failed! \n";
}
elsif( $percentage == 35)
{
printf "Passsed";
}
elsif( $percentage >= 35 && $percentage <= 60)
{
printf "Congrats!! you passed the Exam";
}
elsif( $percentage > 60 && $percentage <= 75)
{
printf "Congrats!! you passed with Distinction";
}
elsif( $percentage > 75 && $percentage <= 90)
{
printf "Congrats!! you passed the Exam with B Grade";
}
elsif( $percentage > 90 && $percentage <= 100)
{
printf "Congrats!! you passed the Exam with A Grade";
}
Output:
Explanation:
- Here we have written a code to test the grade based on the percentage.
- Here we have mentioned various conditions according to the percentage value.
- If the entered percentage value is greater than 100 or less than 0, it will print enter valid percentage.
- If the value is between greater than 0 and less than 35 it will print Failed.
- If the value is exact 35 then it will print passed. Likewise, it tests all the conditions and will print the result accordingly.
Recommended Articles
This is a guide to Perl if statements. Here we discuss the introduction Perl if statement with the types and respective examples for better understanding. You may also have a look at the following articles to learn more –