Perl-if-else-statement
提供:Dev Guides
Perl IF … ELSEステートメント
Perlの if ステートメントの後にオプションの else ステートメントを続けることができます。これはブール式がfalseの場合に実行されます。
構文
Perlプログラミング言語の if … else ステートメントの構文は-
if(boolean_expression) {
# statement(s) will execute if the given condition is true
} else {
# statement(s) will execute if the given condition is false
}
ブール式が true と評価された場合、コードの* ifブロック*が実行されます。そうでない場合、コードの* elseブロック*が実行されます。
数値0、文字列 '0’および ""、空のリスト()、およびundefはすべてブールコンテキストで false であり、他のすべての値は true です。 !*または *not による真の値の否定は、特別な偽の値を返します。
流れ図
例
#!/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 is less than 20\n";
} else {
# if condition is false then print the following
printf "a is greater than 20\n";
}
print "value of a is : $a\n";
$a = "";
# check the boolean condition using if statement
if( $a ) {
# if condition is true then print the following
printf "a has a true value\n";
} else {
# if condition is false then print the following
printf "a has a false value\n";
}
print "value of a is : $a\n";
上記のコードが実行されると、次の結果が生成されます-
a is greater than 20
value of a is : 100
a has a false value
value of a is :