Perl conditional statement with example
home PYTHONJAVA

Perl conditional statement

A Perl conditional statement is a block of code that is executed by the execution result of one or more statements (True or False).

You can use the following figure to briefly understand the execution of conditional statements:

Note that the number 0, the strings '0', "", the empty list () , and the undef are false , and the other values are true. True returns ! or not to return false .

Perl provides drop-down conditional statements:

StatementDescription

if statement

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

if...else statement

A if statement can be followed by an optional else statement, which is executed when the boolean expression is false.

if...elsif.. .else statement

You can follow an if statement followed by an optional elsif statement, and then Follow another else statement.

unless statement

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

unless...else statement.

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

unless...elsif..else Statement

A unless statement can be followed by an optional elsif statement, followed by another else statement.

switch statement

In the latest version of Perl, we can use the switch statement. It executes the corresponding code block according to different values.


Ternary operator ? :

We can use conditional operations ? : to simplify the operation of the if...else statement. Usually the format is:

Exp1 ? Exp2 : Exp3;

If the Exp1 expression is true , the result of the Exp2 expression is returned, otherwise it returns Exp3.

The example is as follows:

Instance

#!/usr/local/bin/perl $name = " yandex Tutorial"; $favorite= 10 ; # likes $status = ( $favorite > 60 )? "Top Sites" : "Not a popular website"; print "$name - $status\n";

Execute the above program, the output is:

yandex Tutorial - Not a popular website





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.