Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl UNLESS…ELSE Statement

Perl UNLESS...ELSE Statement

Perl Conditional Statements Perl Conditional Statements

An unless statement can be followed by an optional else statement, which executes when the Boolean expression is true.

Syntax

The syntax format is as follows:



unless(boolean_expression){

   # Execute when boolean_expression is false

}else{

   # Execute if boolean_expression is true

}

If the Boolean expression boolean_expression is false, the code inside the unless block is executed. If the Boolean expression is true, the code inside the else block is executed.

Flowchart

Example

Example

#!/usr/bin/perl

$a

=

100

;

Using an unless statement to test a Boolean expression

unless

(

$a

==

20

)

{

Executed when the Boolean expression is false

printf

"

The given condition is false

\n

"

;

}

else

{

Executed when the Boolean expression is true

printf

"

The given condition is true

\n

"

;

}

print

"

The value of a is:

$a

\n

"

;

$a

=

"

"

;

Using an unless statement to test a Boolean expression

unless

(

$a

)

{

Executed when the Boolean expression is false

printf

"

a The given condition is false

\n

"

;

}

else

{

Executed when the Boolean expression is true

printf

"

a The given condition is true

\n

"

;

}

print

"

The value of a is:

$a

\n

"

;

Running the above program, the output is:



给定的条件为 false

a 的值为 : 100

a 给定的条件为 false

The value of a is:

Perl Conditional Statements Perl Conditional Statements