Perl 中的 if...elsif...else语句函数

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

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

if...elsif...else - 语法

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

if(boolean_expression 1) {
   # Executes when the boolean expression 1 is true
} 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 true
}

if...elsif...else - 示例

#!/usr/local/bin/perl
 
$a=100;
# check the boolean condition using if statement
if( $a  ==  20 ) {
   # if condition is true then print the following
   printf "a has a value which is 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 true
   printf "a has a value which is $a\n";
}

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

链接:https://www.learnfk.comhttps://www.learnfk.com/perl/perl-if-elsif-statement.html

来源:LearnFk无涯教程网

a has a value which is 100

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

技术教程推荐

深入浅出gRPC -〔李林锋〕

趣谈Linux操作系统 -〔刘超〕

Netty源码剖析与实战 -〔傅健〕

Service Mesh实战 -〔马若飞〕

职场求生攻略 -〔臧萌〕

Web安全攻防实战 -〔王昊天〕

etcd实战课 -〔唐聪〕

打造爆款短视频 -〔周维〕

人人都用得上的数字化思维课 -〔付晓岩〕

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