YouTip LogoYouTip

Perl If Elsif Statement

# Perl IF...ELSIF Statement [![Image 3: Perl Conditions](#) Perl Conditions](#) An `if` statement can be followed by an optional `elsif` statement, which is then followed by another `else` statement. This type of conditional statement is very useful when dealing with multiple conditions. When using `if`, `elsif`, and `else` statements, you need to pay attention to the following points. * An `if` statement can be followed by 0 or 1 `else` statement, but an `else` statement must follow an `elsif` statement. * An `if` statement can be followed by 0 or 1 `elsif` statements, but they must be written before the `else` statement. * If one of the `elsif` conditions is true, the other `elsif` and `else` statements will not be executed. ### Syntax The syntax format is as follows: if(boolean_expression 1){ # Executes when boolean_expression 1 is true}elsif( boolean_expression 2){ # Executes when boolean_expression 2 is true}elsif( boolean_expression 3){ # Executes when boolean_expression 3 is true}else{ # Executes when all boolean expressions are false} ## Example #!/usr/bin/perl$a = 100; # Use == to check if two numbers are equal if($a == 20){# Executes when the condition is true printf"a's value is 20n"; }elsif($a == 30){# Executes when the condition is true printf"a's value is 30n"; }else{# Executes when all the above conditions are false printf"a's value is $an"; } Executing the above program will produce the following output: a's value is 100 [![Image 4: Perl Conditions](#) Perl Conditions](#)
← Perl Unless StatementPerl If Statement β†’