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://www.codercto.com/courses/l/19.html

查看所有标签

Developer's Guide to Social Programming

Developer's Guide to Social Programming

Mark D. Hawker / Addison-Wesley Professional / 2010-8-25 / USD 39.99

In The Developer's Guide to Social Programming, Mark Hawker shows developers how to build applications that integrate with the major social networking sites. Unlike competitive books that focus on a s......一起来看看 《Developer's Guide to Social Programming》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

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

HEX HSV 互换工具