Perl IF Statement
A Perl if statement consists of a boolean expression followed by one or more statements.
Syntax
The syntax is as follows:
if(boolean_expression){
# Executed when boolean_expression is true
}
If the boolean expression boolean_expression
is true, the block of code inside the if statement will be executed. If the boolean expression is false, the first set of code after the if statement (after the closing bracket) will be executed.
Flowchart
Example
#!/usr/bin/perl
$a = 10;
# Using if statement to check the boolean expression
if( $a < 20 ){
# Executed when the boolean expression is true
printf "a is less than 20\n";
}
print "The value of a is : $a\n";
$a = "";
# Using if statement to check the boolean expression
if( $a ){
# Executed when the boolean expression is true
printf "Variable a is true\n";
}
print "The value of a is : $a\n";
Executing the above program will output:
a is less than 20
The value of a is : 10
The value of a is :