Perl 除非语句后可以跟可选的 else 语句,该语句在布尔表达式为true时执行。
Perl编程语言中的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 ,则将执行代码的除非块,否则将执行代码的 else块。
#!/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 :
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)