Perl
conditional statement is the result of the execution of one or more statements
True
or
False
to determine the block of code to execute
You can use the following figure to briefly understand the execution of conditional statements:
Note that the number 0, string
'0'
、
""
, empty
list()
, and
undef
for
false
all other values are
true
.
true
previous use
!
or
not
then return to
false
.
Perl
provide the following conditional statements are provided:
Statement | Description |
|---|---|
If statement | An if statement consists of a Boolean expression followed by one or more statements. |
If…else statement | An if statement can be followed by an optional else statement, and the else statement is executed when the Boolean expression is false. |
If…elsif…else statement | You can follow an if statement with an optional elsif statement, followed byanother else statement. |
Unless statement | A unless statement consists of a Boolean expression followed by one or more statements. |
Unless…else statement. | An unless statement can be followed by an optional else statement. |
Unless…elsif..else statement | An unless statement can be followed by an optional elsif statement, followedby another else statement. |
Switch statement | In the latest version of Perl, we can use the switch statement. It executes the corresponding block of code according to different values. |
5.10.1. Ternary operator?: #
We can use conditional operations
?
:
to simplify
if...else
operation statement. The usual format is:
Exp1 ? Exp2 : Exp3;
If
Exp1
the expression is
true
, then return
Exp2
expression evaluates the result, otherwise returns
Exp3
.
The example is as follows: Execute the above program, and the output is as follows:Example #
#/ Usr/local/bin/perl $name="Rookie Tutorial"$ Favorite=10#
Number of likes $status=($favorite>60)? Popular website ":"
Not a popular website "; Print "$name - $status n";
Novice Tutorial - Not a Popular Website