Perl Conditional Statements - IF...ELSE - Perl Scripting

What is the purpose of conditional statements in Perl?

Like any other programming language, Perl also has conditional statements which helps in the decision making, where the programmer needs to specify 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, however, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the programming languages −
decision_making
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.
S.No. Statement & Description
1. if statement
Anif statementconsists of a boolean expression followed by one or more statements.
2. if...else statement
Anif statementcan be followed by an optionalelse statement.
3. if...elsif...else statement
Anif statementcan be followed by an optionalelsif statementand then by an optionalelse statement.
4. unless statement
Anunless statementconsists of a boolean expression followed by one or more statements.
5. unless...else statement
Anunless statementcan be followed by an optionalelse statement.
6. unless...elsif..else statement
Anunless statementcan be followed by an optionalelsif statementand then by an optionalelse statement.
7. switch statement
With the latest versions of Perl, you can make use of theswitchstatement. which allows a simple way of comparing a variable value against various conditions.

The ? : Operator

Let's check the conditional operator ? :this can be used to replace if...else statements. It has the following general form −
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 how to use of this operator −
This will display the following result −

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Perl Scripting Topics