Conditional statements help in decision making. If a condition is true, this block is executed otherwise the program control moves to the next instruction followed by the block.
In this language, numeric value
0
, string
'0'
or
""
, a list having no elements
()
, and
undef
represent false in a boolean context. Negation of a true value by
!
or not returns a special false value.
In this context, the conditional statements are discussed below.
if statement
A boolean expression is enclosed in
if
, based on the condition either of the the blocks is executed as
shown below.
if...else statement
An if statement can be followed by an optional else statement.
if...elsif...else statement
An
if
statement can be followed by an
elsif
statement, the
elsif
represents
else if
.
unless statement
An
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
Similar to
if...elsif...else
, an unless statement can also be followed by an
elsif
statement, which in turn is followed by an optional
else
statement.
switch statement
In latest versions of Perl,
switch
statement can be used to compare value of a variable against
various conditions.
The ? : Operator
Let's check the conditional operator
? :
which can be used to replace
if...else
statement. It has the general form
Exp1 ? Exp2 : Exp3;
where Exp1, Exp2, and Exp3 are expressions. Notice the use and
placement of the colon.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.