Easy Tutorial
❮ Perl Scalars Perl Until Loop ❯

Perl IF...ELSE Statement

Perl Conditional Statements

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax

The syntax is as follows:

if(boolean_expression){
   # Executed when the boolean_expression is true
}else{
   # Executed when the boolean_expression is false
}

If the boolean expression boolean_expression is true, the code inside the if block is executed. If the boolean expression is false, the code inside the else block is executed.

Flowchart

Example

#!/usr/bin/perl

$a = 100;
# Using if statement to evaluate the boolean expression
if( $a < 20 ){
    # Executed when the boolean expression is true
    printf "a is less than 20\n";
}else{ 
    # Executed when the boolean expression is false
    printf "a is greater than 20\n";
}
print "The value of a is : $a\n";

$a = "";
# Using if statement to evaluate the boolean expression
if( $a ){
    # Executed when the boolean expression is true
    printf "The condition is true for a\n";
}else{
   # Executed when the boolean expression is false
    printf "The condition is false for a\n";
}
print "The value of a is : $a\n";

Executing the above program will output:

a is greater than 20
The value of a is : 100
The condition is false for a
The value of a is :

Perl Conditional Statements

❮ Perl Scalars Perl Until Loop ❯