Perl IF...ELSE Statement

Perl 条件语句Perl Conditional Statements

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax

The syntax format is as follows:

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

If the boolean expression boolean_expression is true, the code inside the if block is executed. If the boolean expression is false, the code inside the else block is executed.

Flowchart

C 中的 if...else 语句

Example

#!/usr/bin/perl $a = 100; # Use an if statement to evaluate the boolean expression if( $a < 20 ){ # Execute when the boolean expression is true printf "a is less than 20\n"; }else{ # Execute when the boolean expression is false printf "a is greater than 20\n"; } print "The value of a is:$a\n"; $a = ""; # Use an if statement to evaluate the boolean expression if( $a ){ # Execute when the boolean expression is true printf "a condition is true\n"; }else{ # Execute when the boolean expression is false printf "a condition is false\n"; } print "The value of a is:$a\n";

Executing the above program, the output is:

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

Perl 条件语句Perl Conditional Statements

Other Extensions