原文: https://beginnersbook.com/2017/02/unless-elsif-else-statement-in-perl/
当我们需要检查多个条件时,使用unless-elsif-else
语句。在这个声明中,我们只有一个unless
和一个else
,但我们可以有多个elsif
。这是它的样子:
unless(condition_1){
#These statements would execute if
#condition_1 is false
statement(s);
}
elsif(condition_2){
#These statements would execute if
#condition_1 & condition_2 are true
statement(s);
}
elsif(condition_3){
#These statements would execute if
#condition_1 is true
#condition_2 is false
#condition_3 is true
statement(s);
}
.
.
.
else{
#if none of the condition is met
#then these statements gets executed
statement(s);
}
#!/usr/local/bin/perl
printf "Enter any number:";
$num = <STDIN>;
unless( $num == 100) {
printf "Number is not 100\n";
}
elsif( $num==100) {
printf "Number is 100\n";
}
else {
printf "Entered number is not 100\n";
}
输出:
Enter any number:101
Number is not 100