First, the condition judgment
if () {
}
elsif () {
}
...
else{
}
Another way to build an I-f statement is to use multiple expressions, then, depending on which expression is true, run the code: You can read the above statement block as follows: If the expression labeled E x P r e s i o 1 is true, then the statement block B L o C K 1 runs. Otherwise, the control is transferred to E L S I f, the E x P r e s i o n 2 is tested, if the expression is true,
Then run b L O C K 2. If E x P r e s i o n 1 and E x P r e s i o n 2 are not true, then B L o C K 3 runs
Second, Cycle:
1, while loop
2, until cycle
3, Class C for the loop, such as
for ($count =1; $count <= 5; $count + +) {
# statements inside the loop go
}
Here is an example of using the comma operator in the For loop:
for ($line =, $count = 1; $count <= 3; $line =, $count + +) {
Print ($line);
}
It is equivalent to the following statement:
$line =;
$count = 1;
while ($count <= 3) {
Print ($line);
$line =;
$count + +;
}
4, for the list (array) of each element of the loop: foreach, the syntax is:
foreach Localvar (listexpr) {
Statement_block;
}
Cases:
foreach $word (@words) {
if ($word eq "the") {
Print ("found the word ' \ n");
}
}
Note:
(1) Here the loop variable Localvar is a local variable, and if it already has a value, the value is restored after the loop.
(2) Change the local variable in the loop, the corresponding array variable will also change, such as:
@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
if ($temp = = 2) {
$temp = 20;
}
}
At this point the @list has become (1, 20, 3, 4, 5).
5. Do Loop
do {
Statement_block
} while_or_until (condexpr);
The Do loop performs at least one loop.
6. Cycle control
The exit loop is last, the same as the break in C; the next loop is the same as the Continue function in C; a Perl-specific command is redo, which means repeating the loop, which is the same as the loop variable, returning to the starting point of the loop, but note that The redo command does not work in the Do loop.
7, the traditional goto label;
Iii. single-line conditions
The syntax is statement keyword CONDEXPR. Where keyword can be if, unless, while, or until, such as:
Print ("This is zero.\n") if ($var = = 0);
Print ("This is zero.\n") unless ($var!= 0);
Print (' not Zero yet.\n ') while ($var-> 0);
Print ("Not Zero yet.\n") until ($var-= = 0);
Although the conditional judgment is written in the back, it is executed first.
directive: unless if not
The meaning of unless is to say, "If the discriminant is not true, execute ...".
Grammar one:
Unless (discriminant expression) {
The discriminant is a false-time statement block;
}
The last syntax can also be written in Perl: The discriminant is a false-time statement block unless (discriminant expression);
Example:
Print "Please enter your score? \ n";
$scorre =; #Stands for standard input, the user is allowed to enter a string
Chop ($score); #将 $score last newline character \ nthe deletion
Unless ($score <60)
{
Print "Your score passed!\n";
}
can also be written: print "Your score passed!\n" unless ($score <60);
Syntax Two:
Unless (discriminant expression)
{
The discriminant is a false-time statement block;
}else{
Statement block when the discriminant is true;
}
Example:
Print "Please enter your score? \ n";
$scorre =;
Chop ($score);
Unless ($score <60)
{
Print "Your score passed!\n";
}else{
Print "Your score is less than lattice!\n";
}
Advanced Skills: &&,| | and?: As a control structure
They look like punctuation marks, or part of an expression. However, it can be used as a control structure in Perl.
For example:
if (discriminant expression)
{is a true time statement block};
Can also be written as:
is true when the statement block if (discriminant expression)
But the simpler way is:
Statement block with discriminant && true
Why, then? && is the logical AND operator, which means:
If the discriminant is true, the value of the expression depends on the value of the subsequent statement block. So the statement block that is true is executed (for evaluation).
If the discriminant is false, the entire expression is false, regardless of the value of the subsequent statement block. So the statement block is not executed when it is false.
In the same way, unless (this) {that} can be replaced with this| | that.
An example of an expression: EXP1?EXP2:EXP3 says: If Exp1 is true, the value of EXP2 is obtained, otherwise EXP3 value is obtained.