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

Perl if Statement

Perl if Statement

Perl Conditional Statements Perl Conditional Statements

A Perl if statement consists of a Boolean expression followed by one or more statements.

Syntax

The syntax format is as follows:



if(boolean_expression){

   # Execute if boolean_expression is true

}

If the boolean expression boolean_expression is true, the code block inside the if statement is executed. If the boolean expression is false, the first group of code after the if statement (after the closing brace) is executed.

Flowchart

Example

#!/usr/bin/perl

$a

=

10

;

Using an if statement to evaluate a Boolean expression

if

(

$a

<

20

)

{

printf

"

a is less 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

"

The variable a is true

\n

"

;

}

print

"

The value of a is:

$a

\n

"

;

Running the above program, the output is:



a 小于 20

a 的值为 : 10

The value of a is:

Perl Conditional Statements Perl Conditional Statements