Perl loop
Sometimes, we may need to execute the same block of code multiple times. Normally, statements are executed sequentially: the first statement in the function is executed first, followed by the second statement, and so on.
Programming languages provide a variety of control structures for more complex execution paths.
Loop statements allow us to execute a statement or statement group multiple times. The following is a flow chart of loop statements in most programming languages:
Note that the number 0, string '0', "", empty list (), and undef are false, and other values are true. If true is preceded by ! or not, false will be returned.
Perl language provides the following loop types:
Loop type | Description |
---|---|
while loop | When the given condition is true, execute the statement or statement group repeatedly. The condition is tested before the loop body is executed. |
until Loop | Repeatedly executes a statement or group of statements until the given condition is true. The condition is tested before the loop body is executed. |
for loop | Execute a sequence of statements multiple times to simplify the code for managing loop variables. |
foreach loop | The foreach loop is used to iterate the values of a list or set variable. |
do...while loop | It is similar to the while statement except that it tests the condition at the end of the loop body. |
Nested Loops | You can use one or more loops inside a while, for, or do..while loop. |
Loop control statement
Loop control statement changes the execution order of the code, through which you can realize code jumps.
Perl provides the following loop control statements:
Control statement | Description |
---|---|
next statement | Stop executing the statements from the next statement of the next statement to the end identifier of the loop body, go to the continue statement block, and then return to the loop body. Start executing the next cycle from the beginning. |
#last statement | Exit the loop statement block, thereby ending the loop |
continue Statement | continue statement block is usually executed before the conditional statement is evaluated again. |
redo statement | The redo statement directly goes to the first line of the loop body and starts to execute this loop repeatedly. The statements after the redo statement do not If executed again, the continue statement block will no longer be executed; |
#goto statement | Perl has three goto forms: got LABLE, goto EXPR, and goto&NAME. |
Infinite Loop
If the condition never becomes false, the loop will become an infinite loop.
The for loop can be used to implement infinite loops in the traditional sense.
Since none of the three expressions that make up the loop are required, you can leave some of the conditional expressions blank to form an infinite loop.
#!/usr/bin/perl for( ; ; ) { printf "循环会无限执行。\n"; }
You can press Ctrl + C keys to terminate the loop.
When the conditional expression does not exist, it is assumed to be true . You can also set an initial value and increment expressions, but in general, Perl programmers prefer to use the for(;;) construct to represent an infinite loop.