目录

unless...elsif..else statement

unless声明后面跟一个可选的elsif...else语句,这对于使用single除非... elsif语句测试各种条件非常有用。

当使用除非,elsif,else语句时,要记住几点。

  • 一个unless可以有零或一个else的,它必须在任何elsif之后。

  • unless可以有零到多个elsif ,他们必须先到else

  • 一旦elsif成功,剩下的elsifelse不会被测试。

语法 (Syntax)

Perl编程语言中的unless...elsif...else语句的语法是 -

unless(boolean_expression 1) {
   # Executes when the boolean expression 1 is false
} elsif( boolean_expression 2) {
   # Executes when the boolean expression 2 is true
} elsif( boolean_expression 3) {
   # Executes when the boolean expression 3 is true
} else {
   # Executes when the none of the above condition is met
}

例子 (Example)

#!/usr/local/bin/perl
$a = 20;
# check the boolean condition using if statement
unless( $a  ==  30 ) {
   # if condition is false then print the following
   printf "a has a value which is not 20\n";
} elsif( $a ==  30 ) {
   # if condition is true then print the following
   printf "a has a value which is 30\n";
} else {
   # if none of the above conditions is met
   printf "a has a value which is $a\n";
}

这里我们使用等于运算符==,它用于检查两个操作数是否相等。 如果两个操作数相同则返回true,否则返回false。 执行上述代码时,会产生以下结果 -

a has a value which is not 20
↑回到顶部↑
WIKI教程 @2018