An if statement can be followed by an optional else statement, which is executed when the Boolean expression is false.
grammar
The syntax format is as follows:
if(boolean_expression){ # execute when boolean expression boolean_expression is true}else{ # execute when boolean expression boolean_expression is false}
If the Boolean expression boolean_expression is true, the code within the if block is executed. If the Boolean expression is false, the code within the else block is executed.
flow chart
Example
#!/usr/bin/perl $a = 100 ; # Use if statement to judge Boolean expression if ( $a < 20 ) { #Executed when the Boolean expression is true printf " a is less than 20 n " ; } else { #Executed when the Boolean expression is false printf " a is greater than 20 n " ; } print " The value of a is: $a n " ; $a = " " ; # Use if statements to determine Boolean expressions if ( $a ) { #Executed when the Boolean expression is true printf " a condition is true n " ; } else { #Executed 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 result is:
The value of a greater than 20a is: 100a The value of the condition is false is: