I. Condition Determination
If (<expression> ){
<Statement_block_1>
}
Elsif (<expression> ){
<Statement_block_2>
}
...
Else {
<Statement_block_3>
}
Ii. Loop:
1. While Loop
While (<expression> ){
<Statement_block>
}
2. Until Loop
Until (<expression> ){
<Statement_block>
}
3. For Loop of class C, such
For ($ COUNT = 1; $ count <= 5; $ count ++ ){
# Statements inside the loop go here
}
The following is an example of using the comma operator in a for loop:
For ($ line = <stdin>, $ COUNT = 1; $ count <= 3; $ line = <stdin>, $ count ++ ){
Print ($ line );
}
It is equivalent to the following statement:
$ Line = <stdin>;
$ COUNT = 1;
While ($ count <= 3 ){
Print ($ line );
$ Line = <stdin>;
$ Count ++;
}
4. Loop for each element in the list (array): foreach. Syntax:
Foreach localvar (listexpr ){
Statement_block;
}
Example:
Foreach $ word (@ words ){
If ($ word EQ ""){
Print ("found the word 'The' \ n ");
}
}
Note:
(1) The loop variable localvar is a local variable. If it already has a value before, it will be restored after the loop.
(2) Change the local variable in the loop, and the corresponding array variable will also change, for example:
@ List = (1, 2, 3, 4, 5 );
Foreach $ temp (@ list ){
If ($ temp = 2 ){
$ Temp = 20;
}
}
At this time, @ list has been changed to (1, 20, 3, 4, 5 ).
5. Do Loop
Do {
Statement_block
} While_or_until (condexpr );
The do loop must be executed at least once.
6. Loop Control
The exit loop is last, which is the same as the break in C. Execute the next loop as next, which is the same as the continue in C. A special command of Perl is redo, which means to repeat this loop, that is, the loop variable remains unchanged and returns to the cycle start point. Note that the redo command does not work in the do loop.
7. Traditional goto label statements.
3. Single Row Condition
The syntax is statement keyword condexpr. The keyword can be if, unless, while, or until, for example:
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 condition judgment is written later, it is executed first.