Perl 中的 unless...else 语句函数

首页 / Perl入门教程 / Perl 中的 unless...else 语句函数

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

unless...else - 语法

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块。

unless...else - 流程图

Perl unless...else statement

unless...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 : 

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言从入门到实战 -〔蔡超〕

SQL必知必会 -〔陈旸〕

Node.js开发实战 -〔杨浩〕

爆款文案修炼手册 -〔乐剑峰〕

Go 语言项目开发实战 -〔孔令飞〕

如何讲好一堂课 -〔薛雨〕

超级访谈:对话张雪峰 -〔张雪峰〕

林外 · 专利写作第一课 -〔林外〕

云原生基础架构实战课 -〔潘野〕

好记忆不如烂笔头。留下您的足迹吧 :)