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 group of statements multiple times. Here is the flow chart of a loop statement in most programming languages:
Note that the number 0, string '0', "", empty list (), and undef are false , and all other values are true . If true is used before ! or not , false will be returned.
The Perl language provides the following loop types:
Loop type | describe |
---|---|
while loop | Repeatedly executes a statement or group of statements when a given condition is true. The condition is tested before the loop body is executed. |
until loop | Repeatedly execute a statement or group of statements until a given condition is true. The condition is tested before the loop body is executed. |
for loop | Execute a sequence of statements multiple times, simplifying code that manages loop variables. |
foreach loop | The foreach loop is used to iterate over the values of a list or collection variable. |
do...while loop | Similar to a 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 statements change the execution order of the code, through which you can achieve code jumps.
Perl provides the following loop control statements:
control statement | describe |
---|---|
next statement | Stop executing the statements starting from the next statement of the next statement to the end identifier of the loop body, transfer to the execution of the continue statement block, and then return to the beginning of the loop body to start executing the next loop. |
last statement | Exit the loop statement block, thereby ending the loop |
continue statement | The continue statement block is usually executed before the conditional statement is evaluated again. |
redo statement | The redo statement goes directly to the first line of the loop body and starts executing this loop repeatedly. The statements after the redo statement will no longer be executed, and the continue statement block will no longer be executed; |
goto statement | Perl has three forms of goto: got LABLE, goto EXPR, and goto &NAME. |
If the condition never becomes false, the loop will become an infinite loop.
The for loop can be used in the traditional sense to implement infinite loops.
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.
You can press Ctrl + C keys to terminate the loop.
When the conditional expression is not present, it is assumed to be true. You can also set an initial value and increment expressions, but generally, Perl programmers prefer to use the for(;;) construct to represent an infinite loop.