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

Perl IF…ELSE Statement

Perl IF...ELSE Statement

Perl Conditional Statements 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){

   # Execute if boolean_expression is true

}else{

   # Execute when boolean_expression is 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

if...else Statements in C

Example

#!/usr/bin/perl

$a

=

100

;

Using an if statement to evaluate a Boolean expression

if

(

$a

<

20

)

{

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

=

"

"

;

Using an if statement to evaluate a Boolean expression

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

"

;

Running the above program, the output is:



a 大于 20

a 的值为 : 100

a 条件为 false

The value of a is:

Perl Conditional Statements Perl Conditional Statements