if-else Statement

Let's discuss if-else statements in detail, including nested-ifs with examples.

Branches in Perl

We often need the execution of some portions of our code to be subject to a certain condition(s). Within the Perl programming language, the simplest and sometimes the most useful way of creating a condition within your program is through an if-else statement.

The if-else statement

Let’s start by taking a look at an example which checks if a variable $a is greater than or less than $b:

Press + to interact
$a = 50; #change the value of "a" so its less than b in order to execute the else statement
$b = 15;
if ($a > $b) {
#this code is executed only if $a is greater than $b
print "\$a is greater than \$b";
}
else {
#this code is executed if the preceding "if" condition evaluated to false
print "\$a is less than \$b";
}

The following flowchart illustrates the execution of the above code:

Explanation

Line 1 & 2:

  • Two variables, $a and $b, are being initialized with the sample values.

Line 4:

  • if is a keyword representing the if statement followed by a condition in the parentheses. This condition is evaluated as true or false depending on the relational operator being used and values of the variables being compared. In this condition, the greater than operator has been used.

  • { represents the start of the block of the if-statement.

Line 5:

  • This line is a comment inside the body of the if-statement.

Line 6:

  • This line of code executes only when $a is greater than $b and prints it on the console.

Line 7:

  • The right curly brace } is essential if there is a left curly brace. It matches the opening brace on line 4 and represents the end of the if body.

  • In case of only one statement in the body of the if statement, the opening and closing braces can be skipped.

Line 8:

  • The else statement must follow an if statement. It executes the statements in its body if the condition inside the if-statement is false. { represents the opening of the body of the else-statement.

Line 9 & 10:

  • This is the body of the else-statement.

Line 11:

  • } represents the end of the body of the else-statement and matches the { on line 8.

The body of the else block can contain another if-statement. This is known as a nested conditional.

It is a good practice to use the same type of variables in comparisons (i.e., not comparing the floating-point values to the characters).

Nested if-statements

One if-statement can be included in the body of another if-statement for a different condition. However, we should be careful to close the body of the inner if-statement before closing the body of the outer if-statement.

In the following example, we take an integer and check in the outer if condition to see whether the number is greater than 0. If the outer if-statement is true, then the inner if-statement checks its condition. If the number is even, then the inner condition will be true. Otherwise, the else statement of the inner if-statement will be executed.

Try the following example and check the execution of different branches by changing the value of the input variable $number.

Press + to interact
$number = 18; # play around by changing values
if ($number > 0) {
print "$number is greater than 0\n";
if($number % 2 == 0){
print "$number is even!\n"
}#end of inner if
else{
print "$number is odd!\n"
}#end of inner else
}#end of outer if
else{
print "$number is less than 0"
}#end of outer else