Latest web development tutorials

Perl IF statement

Perl conditional statements Perl conditional statements

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

grammar

The syntax is as follows:

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

If the Boolean expression boolean_expression true, the if statement within the code block will be executed. If the Boolean expression is false, if the first set of codes after the statement (after the close parenthesis) will be executed.

flow chart

Perl if statement

Examples

#!/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 program, the output is:

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

Perl conditional statements Perl conditional statements