YouTip LogoYouTip

Perl Unless Elsif Statement

# Perl UNLESS...ELSIF Statement [![Image 3: Perl Conditional Statements](#) Perl Conditional Statements](#) An `unless` statement can be followed by an optional `elsif` statement, and then another `else` statement. This type of conditional statement is very useful when dealing with multiple conditions. When using `unless`, `elsif`, and `else` statements, you need to note the following points. * An `unless` statement can be followed by 0 or 1 `else` statement, but an `elsif` must be followed by an `else` statement. * An `unless` 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 met, the other `elsif` and `else` blocks will not be executed. ### Syntax The syntax format is as follows: unless(boolean_expression 1){ # Executes when boolean_expression 1 is false}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 no condition matches} ## Example #!/usr/bin/perl$a = 20; # Use unless statement to check boolean expression unless($a == 30){# Executes when boolean expression is false printf "a's value is not 30n"; }elsif($a == 30){# Executes when boolean expression is true printf "a's value is 30n"; }else{# Executes when no condition matches printf "a's value is $an"; } Executing the above program, the output result is: a's value is not 30 [![Image 4: Perl Conditional Statements](#) Perl Conditional Statements](#)
← Perl While LoopPerl Unless Statement β†’