Latest web development tutorials

Perl IF ... ELSE statement

Perl conditional statements Perl conditional statements

After a if statement with an optional else statement, else statement executes the Boolean expression is false.

grammar

The syntax is as follows:

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

If the Boolean expression boolean_expression true, the block of code if the execution. If the Boolean expression is false, then the code within the else block executed.

flow chart

C in the if ... else statement

Examples

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

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

The above program, the output is:

a 大于 20
a 的值为 : 100
a 条件为 false
a 的值为 :

Perl conditional statements Perl conditional statements