Perl 中的 unless...elsif..else 语句

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

除非unless语句后可以跟可选的 elsif ... else 语句,这对于使用单个unless... elsif语句测试各种条件非常有用。

unless...elsif..else - 语法

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

unless(boolean_expression 1) {
   # Executes when the boolean expression 1 is false
} elsif( boolean_expression 2) {
   # Executes when the boolean expression 2 is true
} elsif( boolean_expression 3) {
   # Executes when the boolean expression 3 is true
} else {
   # Executes when the none of the above condition is met
}

unless...elsif..else - 示例

#!/usr/local/bin/perl
 
$a=20;
# check the boolean condition using if statement
unless( $a  ==  30 ) {
   # if condition is false then print the following
   printf "a has a value which is not 20\n";
} elsif( $a ==  30 ) {
   # if condition is true then print the following
   printf "a has a value which is 30\n";
} else {
   # if none of the above conditions is met
   printf "a has a value which is $a\n";
}

在这里,我们使用等于运算符==,用于检查两个操作数是否相等。如果两个操作数相同,则返回true,否则重新运行为false。

a has a value which is not 20

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

技术教程推荐

深入拆解Java虚拟机 -〔郑雨迪〕

Vue开发实战 -〔唐金州〕

JavaScript核心原理解析 -〔周爱民〕

分布式协议与算法实战 -〔韩健〕

A/B测试从0到1 -〔张博伟〕

程序员的测试课 -〔郑晔〕

深入浅出分布式技术原理 -〔陈现麟〕

技术领导力实战笔记 2022 -〔TGO 鲲鹏会〕

B端体验设计入门课 -〔林远宏(汤圆)〕

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