Latest web development tutorials

Perl UNLESS ... ELSIF statement

Perl conditional statements Perl conditional statements

Unless after a statement with an optional elsif statement, and then followed by another else statement.

This conditional statement is very useful in the case of multiple conditions.

Use unless, elsif, else statement when you need to pay attention to the following points.

  • After the statement unless it can keep up with 0 or an else statement, but there must be behind elsif else statements.

  • After the statement unless you can keep up with 0 or 1 elsif statement, but they must be written before the else statement.

  • If one elsif executed successfully, and other elsif else will no longer be executed.

grammar

The syntax is as follows:

unless(boolean_expression 1){
   # 在布尔表达式 boolean_expression 1 为 false 执行
}
elsif( boolean_expression 2){
   # 在布尔表达式 boolean_expression 2 为 true 执行
}
elsif( boolean_expression 3){
   # 在布尔表达式 boolean_expression 3 为 true 执行
}
else{
   #  没有条件匹配时执行
}

Examples

#!/usr/bin/perl

$a = 20;
# 使用 unless 语句检测布尔表达式
unless( $a  ==  30 ){
    # 布尔表达式为 false 时执行
    printf "a 的值不为 30\n";
}elsif( $a ==  30 ){
    # 布尔表达式为 true 时执行
    printf "a 的值为 30\n";
}else{
    # 没有条件匹配时执行
    printf "a  的 值为 $a\n";
}

The above program, the output is:

a 的值不为 30

Perl conditional statements Perl conditional statements