Perl IF statement

Perl IF statement Perl conditional statement

A Perl if statement consists of a Boolean expression followed by one or more statements.

Grammar

The syntax format looks like this:

if(boolean_expression){
   # 在布尔表达式 boolean_expression 为 true 执行
}

If the Boolean expression boolean_expression true, the block of code within the if statement is executed. If the Boolean expression is false, the first set of code (after parentheses) after the if statement ends is executed.

Flow chart

Perl IF statement

Instance

#!/usr/bin/perl
 
$a = 10;
# 使用 if 语句判断布尔表达式
if( $a < 20 ){
    # 布尔表达式为true时执行
    printf "a 小于 20\n";
}
print "a 的值为 : $a\n";

$a = "";
# 使用 if 语句判断布尔表达式
if( $a ){
    # i布尔表达式为true时执行
    printf "变量 a 为 true\n";
}
print "a 的值为 : $a\n";

The above procedure is performed and the output is:

a 小于 20
a 的值为 : 10
a 的值为 : 

Perl IF statement Perl conditional statement