1. Types of statements
Computers are a tool that has the advantage of performing repetitive tasks. Loop execution and loop statements.
A large number of judgments are required, and the conditional statements are executed according to conditions.
Sequential statements.
Summary of the true and false expression values
Expressions are not necessarily logical expressions, but they must obtain true and false values.
False value:
The logic value is false
Value is 0
The string is empty
List is empty
undef
Other cases are true
2. Conditional statement if
form
If(expression){
if(expression)
Statement}
elsif(expression){ Statement
}
statement}
….
else{
Statement
}
illustrate:
(1) The elsif and else parts can be omitted.
(2) Pay attention to the elsif keyword.
(3) Statement block {} must have, but can be empty
(4) Can be nested.
(5) Meaning: Execute when it is true, and the expression is an execution condition.
3. Conditional statement unless
unless(expression){statement}
Contrary to the meaning of if condition, the execution statement exits unless the expression is true and is true, and the expression is an exit condition.
4. Loop statement while
while(expression){statement}
(1) Execution is a condition for execution when the expression is true.
(2) The condition must be set to be false, otherwise it cannot be exited and the loop cannot be restricted.
do{statement}while(expression)
Execute at least once, first and then make a judgment.
5. Loop statement until
until (expression){statement}
(1) Contrary to while, execute the statement until the expression is true.
(2) Stop when true, and the expression is an exit condition.
do{statement}until expression)
Execute the statement at least once before making conditional judgments.
6. For loop
for(Expression 1;Expression 2;Expression 3){Statement}
(1) Expression 1: Initialize the control variable or other variable, only executed during the first loop, can be
Comma separated assign values to multiple variables.
(2) Expression 2: Compare each time the loop, loop when it is true.
(3) Expression 3: Execution is only performed after each execution of the loop. Change the value of the control variable, which is generally self-incremented.
(4) All three expressions can be omitted, but two; must be preserved.for(;;)
(5) First, all three expressions can be used to execute multiple statements.
For example: for($line=<>,$count=1;$count<10;$line=<>,$count++){print $line;}
7. Foreach statement
foreach $w (list or array){statement}
(1) In () is the array @a, or list (1,2,3)
(2) $w can be a new variable or a used variable, and the variable value is restored after the loop ends.
(3) Each loop is used to assign the values in the list or array to $w in sequence until the list is empty, so you don’t have to worry about the length of the array.
(4) If the value of $w is modified in the loop body, the corresponding data element value will also change.
(5) When a constant in the list is a value of $w cannot be modified.
(6) $w and @a can be the same, such as $a(@a)
(7) For and foreach can be interchangeable and general.
Foreach form
(1) foreach $a(@a){} General form.
(2) foreach $a(1,2,3,4){} List
(3) foreach $k(keys%h){} hash form
(4) foreach $a(@a[1,2,3]){} Only partial elements of the array
(5) foreach (@a){} Omit the loop variable, the default is $_
$_default variable, print is to print $_variable.
(6) grep,map function is equivalent to foreach operating on each element.
8. Cycle control
last: Exit the loop
next: Execute the next loop. The statements under next in the loop are not executed this time. For for, the variables need to be self-increased.
redo: Re-execute this loop. For the for-not performing variable self-increment, it is invalid in the do statement and is not recommended.
goto: Jump, it cannot jump to loops or subroutines. It is not recommended to use structured programming now.
9. Mark
Markers can only be composed of letters and underscores, generally in capital, separated by:.
Tag definition: Definition before or before a loop statement (goto)
Generally, when the inner layer of the nested loop jumps to the outermost layer, the last exits the outermost loop, and the program continues to go down.
It's not to jump to the outermost layer before executing.
example:
#!/usr/bin/perl
print "Use tags:";
LABEL:for($i=1;$i<5;$i++){
for($j=1;$j<5;$j++){
print “$j”;
last LABEL if $i==3;
}
}
print “\$i=$i”;
10. Continue statement block
Continue{Statement}
(1) Immediately after the loop statement, execute before the loop body is executed and the next condition judgment is made.
(2) Can also be used after statement blocks identified by {}.
(3) No execution after jumping from last, but next execution.
11. Single-line conditional statements and loop statements
Conditional statement: The statement is first and the condition is after.print $a if $a==0; but test the conditions first and then execute the statement.
Similarly:
unless: print $a unless($a==0);
while: print $a while($a–==0);
Note: Do not lose control variables in loop statements, causing a dead loop.
Use the conditional statement of ||,&&: $a==0&&print $a;
open(F,'file')||die “can't open”;
die function: Exit the program after the console standard error outputs information.
warning: After outputting information, the program will not be exited, it will only serve as an alarm.
$!: Internal variable contains error code.
$@:Internal variables contain error information.
12. Summary
1. Conditional statement:
if (expression){statement}
elsif(expression){statement}
….
else{statement}
2. Single-line statement if (expression);
3.|| or && and form
4.?:Form
(Expression){Statement}
Single-line statement unless (expression);
until loop
while(expression){statement}
do{statement}while(expression)
while(expression){statement}continue{statement}
Single line statement while (expression)
until (expression){statement}
do{statement}until(expression)
until (expression){statement}continue{statement}
Single line statement until (expression)
foreach loop
for(Expression 1;Expression 2;Expression 3){Statement}
foreach (@a){statement}
foreach $k(keys%hash){ statement}
8. Loop control: last, next, redo
9. Tag: goto LABEL