当前位置: 首页 > 文档资料 > Perl 入门教程 >

unless...else statement

优质
小牛编辑
140浏览
2023-12-01

Perl unless语句后面可以跟一个可选的else语句,该语句在布尔表达式为true时执行。

语法 (Syntax)

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

unless(boolean_expression) {
   # statement(s) will execute if the given condition is false
} else {
   # statement(s) will execute if the given condition is true
}

如果布尔表达式的计算结果为true那么将执行unless block代码unless block ,否则将执行代码else block

数字0,字符串'0'和“”,空列表()和undef在布尔上下文中都是false ,所有其他值都为true 。 否定真正的价值!not返回特殊的假值。

流程图 (Flow Diagram)

Perl除非...... else语句

例子 (Example)

#!/usr/local/bin/perl
$a = 100;
# check the boolean condition using unless statement
unless( $a == 20 ) {
   # if condition is false then print the following
   printf "given condition is false\n";
} else { 
   # if condition is true then print the following
   printf "given condition is true\n";
}
print "value of a is : $a\n";
$a = "";
# check the boolean condition using unless statement
unless( $a ) {
   # if condition is false then print the following
   printf "a has a false value\n";
} else {
   # if condition is true then print the following
   printf "a has a true value\n";
}
print "value of a is : $a\n";

执行上述代码时,会产生以下结果 -

given condition is false
value of a is : 100
a has a false value
value of a is :