This tutorial explains how to use if-else conditional statements in Perl with examples.
if conditional statements are used to execute code blocks based on a conditional expression. Perl provides the following conditional statements
It provides the following features
- simple if statements
- if else statements
- if else if else
- nested if else
Simple Perl if conditional statement
if the conditional statement is used to execute code block based on conditional expression statements.
Syntax:
if condition_expression
//code block
-The condition_expression
contains either single
or multiple
conditions. Multiple conditions are used with logical operators.
condition_expression
is always evaluated as true
or false
. if it is true
, It executes a code block.
Here is an example
if(conditional_expression){
code_statement;
code_statement;
code_statement;
}
if
is a keyword in Perl- conditional expression are always enclosed
()
and must require for simple condition - code statements are enclosed in
{}
and it is compulsory to include for single statement also
conditional_expression
is a Boolean expression that returns true
or false
.
- if the condition is true, the code block is executed.
- if the condition is false, the code block is not executed. Here is an example of Perl if statements example for checking even numbers.
my $number=20;
if ($number%2==0) {
print("Even Number")
}
Output:
Even Number
Here is a simple if conditional statement example for Odd numbers
my $number=21;
if ($number%2!=0) {
print("Odd Number")
}
Output:
Odd Number
Perl if else conditional statements
if else
in Perl is used to execute code statements based on true
and false
conditional expressions.
Syntax:
if (condition){
//code block based on true conditional value
}
else{
// code block based on false conditional value
}
condition is a Boolean expression that returns true
or false
- if the condition is
true
, code block inside if block is executed. - if the condition is
false
, the code block inside the else block is executed. Here is an example of Perl if conditional true example
Here is a Perl if else conditional expression example
my $number=11;
if ($number%2 == 0) {
print("Even Number")
} else {
print("Odd Number")
}
Output:
Odd Number
Perl if else if statement example
This is a combination of if-else
and if
statements.
This is used to execute code blocks based on multiple conditions.
Syntax:
if (condition1){
code block executes based on the condition1 that is true
}
elsif (condition2) {
code block executes based on the condition2 that is true
}
else{
code block executes based on the condition1 is false
}
Here is an example
my $number=8181;
if ($number < 9) {
print("Single-digit number")
}
elsif (($number > 9) && ($number<99)) {
print("Two digit number")
}
elsif (($number > 99) && ($number<999)) {
print("Three digit number")
}
else {
print("Four digit number")
}
Output:
Four digit number
perl if condition with and operator
The conditional expression contains logical Operator
Logic And operator( and
or &&)
Logic OR operator( or
or ||
)
Logic Not operator( not
)
In the above example, the Logical And operator are used in multiple conditions. It returns true if both conditions are true, else returns false.
my $number=76;
if ($number < 9) {
print("Single-digit number")
}
elsif (($number > 9) && ($number<99)) {
print("Two digit number")
}
elsif (($number > 99) && ($number<999)) {
print("Three digit number")
}
else {
print("Four digit number")
}
Perl If condition string example
We can check whether given two strings are equal or not using Perl if the conditional statement
Strings are equal or not using ==
and eq operator
in If statements.
==
is numeric comparision operator, that evaluates both operands to numeric value(0) and compare its value.- The
eq operator
is a string comparison operator that compares two string characters that are equal with the exact case.
my $str="test";
# using == operator
if ( $str == "test") {
print("string are equal using == \n")
}
# using the eq operator
if ( $str eq "test") {
print("string are equal using eq")
}
## using the eq operator
if ( $str eq "Test") { # evaluates to false, both are different in case-sensitive
print("string are equal using eq")
}
Output:
string are equal using ==
string are equal using eq
Perl one line if statement
Perl supports if-else statements in a single line using the ternary operator
For example, here is an if-else statement in Perl.
sub find_age {
my $age = 40;
my $result;
if ($age >= 60) {
$result = "Senior Citizen";
}
else {
$result = "Not Senior Citizen";
}
return $result;
}
The same above can be rewritten using short hand ternary conditional operator
my $age=40
print ($age < 10) ? "Child" : ($age < 18) ? "Teenage" : ( $age <60> 18) ? "Man" : "Senior";