Perl Loops
Sometimes, we may need to execute the same block of code multiple times. Generally, statements are executed in order: the first statement in a function executes first, followed by the second statement, and so on.
Programming languages provide various control structures that allow for more complex execution paths.
Loop statements allow us to execute a statement or a group of statements multiple times. Below is a flowchart of loop statements in most programming languages:

Note that the number 0, the string '0', "", the empty list (), and undef arefalse, while other values aretrue. Before true, using!ornotreturns false.
The Perl language provides the following loop types:
| Loop Type | Description |
|---|---|
| Repeats a statement or group of statements while the given condition is true. It tests the condition before executing the loop body. | |
| Repeats a statement or group of statements until the given condition is true. It tests the condition before executing the loop body. | |
| Executes a sequence of statements multiple times, simplifying the code that manages the loop variable. | |
| The foreach loop is used to iterate over the values of a list or collection variable. | |
| Similar to the while statement, except that it tests the condition at the end of the loop body. | |
| You can use one or more loops inside a while, for, or do...while loop. |
Loop Control Statements
Loop control statements change the execution order of code, allowing you to implement jumps in the code.
Perl provides the following loop control statements:
| Control Statement | Description |
|---|---|
| Stops executing the statements from the one following the next statement up to the end-of-loop-body marker, transfers execution to the continue block, and then returns to the beginning of the loop body to start the next iteration. | |
| Exits the loop statement block, thereby ending the loop. | |
| The continue block is usually executed before the condition is evaluated again. | |
| The redo statement directly jumps to the first line of the loop body to repeat this iteration; statements after the redo statement are no longer executed, and the continue block is not executed either. | |
| Perl has three goto forms: goto LABEL, goto EXPR, and goto &NAME. |
Infinite Loops
If the condition never becomes false, the loop becomes an infinite loop.
The for loop can traditionally be used to implement an infinite loop.
Since none of the three expressions that constitute a loop is required, you can construct an infinite loop by leaving some conditional expressions empty.
Examples
You can press Ctrl + C to terminate the loop.
When the condition expression is absent, it is assumed to be true. You can also set an initial value and an increment expression, but in general, Perl programmers prefer to use the for(;;) construct to represent an infinite loop.
Other Extensions