Perl – IF…ELSE


Perl Conditional Statements – IF…ELSE


”;


Perl conditional statements helps in the decision making, which require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Following is the general from of a typical decision making structure found in most of the programming languages −

Decision making statements in Perl

The number 0, the strings ”0” and “” , the empty list () , and undef are all false in a boolean context and all other values are true. Negation of a true value by ! or not returns a special false value.

Perl programming language provides the following types of conditional statements.

Sr.No. Statement & Description
1 if statement

An if statement consists of a boolean expression followed by one or more statements.

2 if…else statement

An if statement can be followed by an optional else statement.

3 if…elsif…else statement

An if statement can be followed by an optional elsif statement and then by an optional else statement.

4 unless statement

An unless statement consists of a boolean expression followed by one or more statements.

5 unless…else statement

An unless statement can be followed by an optional else statement.

6 unless…elsif..else statement

An unless statement can be followed by an optional elsif statement and then by an optional else statement.

7 switch statement

With the latest versions of Perl, you can make use of the switch statement. which allows a simple way of comparing a variable value against various conditions.

The ? : Operator

Let”s check the conditional operator ? :which can be used to replace if…else statements. It has the following general form −

Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.

The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression. Below is a simple example making use of this operator −

#!/usr/local/bin/perl
 
$name = "Ali";
$age = 10;

$status = ($age > 60 )? "A senior citizen" : "Not a senior citizen";

print "$name is  - $statusn";

This will produce the following result −

Ali is - Not a senior citizen

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *