Perl-unless-else-statement
提供:Dev Guides
Perl UNLESS … ELSEステートメント
Perl unless ステートメントの後にオプションの 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 と評価された場合、コードの* unlessブロック*が実行されます。そうでない場合、* elseブロック*のコードが実行されます。
数値0、文字列 '0’および ""、空のリスト()、およびundefはすべてブールコンテキストで false であり、他のすべての値は true です。 !*または *not による真の値の否定は、特別な偽の値を返します。
流れ図
例
#!/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 :