Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the instructions of the selective control structure of Perl

Shulou Source: shulou.com Published: 2022-06-02 07:56:13 10月01日 Update

This article shares with you the contents of the instructions on the selective control structure of Perl. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Perl selective control structure

The selective control structure includes if and unless statements, similar to if in C language.

First of all, let's introduce the definition of true and false. In Perl, these rules are somewhat difficult to understand. When the value of the control expression is an empty string or 0, the expression is false, and in any other case, the value of the expression is true.

Note: "00" is not an empty string or 0, the value is true. The same is true of "0.0".

Instruction: if if

Grammar one:

If (discriminant expression)

{

A block of statements when the expression is true

}

The previous syntax can also be written in Perl as: if (discriminant expression) when the expression is true.

Example:

Print "Please enter your score?\ n"; $scorre=;# stands for standard input and will allow the user to enter a string chop ($score); # remove a newline character from $score***\ nDelete if ($score > = 60) {print "your score passed!\ n";}

It can also be written as: print "your score passed!\ n" if ($score > = 60)

Grammar 2:

If (discriminant formula 1) {

The block of statements when discriminant one is true

} else {

A statement block when discriminant one is false.

}

Example:

Print "Please enter your score?\ n"; $scorre=; chop ($score); if ($score > = 60) {print "your score passed!\ n";} else {print "your score failed!\ n";}

Grammar 3:

If (discriminant formula 1)

{

The block of statements when discriminant one is true

} elsif (discriminant formula 2) {

The statement block when discriminant 2 is true

} elsif (discriminant expression 3) {

The statement block when discriminant 3 is true

} else {

Statement blocks when all discriminants are false

}

Example:

Print "Please enter your score?\ n"; $scorre=; chop ($score); if ($score > 60) {print "your score is greater than 60!\ n";} elsif ($score)

Tags: Discriminant score statement discriminant formula input grammar control example expression instruction structure selectivity selection character representative user content string more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Shulou Tech Info Docker NVidia