Perl UNLESS...ELSIF 语句

Perl 教程 · 2019-02-26 20:41:24

一个 unless 语句后可跟一个可选的 elsif 语句,然后再跟另一个 else 语句。

这种条件判断语句在多个条件的情况下非常有用。

在使用 unless , elsif , else 语句时你需要注意以下几点。

  • unless 语句后可以跟上 0 个 或 1 个 else 语句,但是 elsif 后面必须有 else 语句。

  • unless 语句后可以跟上 0 个 或 1 个 elsif 语句,但它们必须写在 else 语句前。

  • 如果其中的一个 elsif 执行成功,其他的 elsif 和 else 将不再被执行。

语法

语法格式如下所示:

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{
   #  没有条件匹配时执行
}

实例

#!/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"; }

执行以上程序,输出结果为:

a 的值不为 30

点击查看所有 Perl 教程 文章: https://codercto.com/courses/l/19.html

查看所有标签

Speed Up Your Site

Speed Up Your Site

Andrew B. King / New Riders Press / 2003-01-14 / USD 39.99

There's a time bomb on the web: user patience. It starts ticking each time someone opens one of your pages. You only have a few seconds to get compelling content onto the screen. Fail, and you can kis......一起来看看 《Speed Up Your Site》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具