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

> Note that the number 0, the string '0', "", the empty list (), and undef are **false**; all other values are **true**. Using **!** or **not** before a true value returns false.
Perl provides the following types of loops:
| Loop Type | Description |
| --- | --- |
| (#) | Repeats a statement or group of statements as long as a given condition is true. The condition is tested before the loop body is executed. |
| (#) | Repeats a statement or group of statements until a given condition becomes true. The condition is tested before the loop body is executed. |
| (#) | Executes a sequence of statements multiple times, simplifying the code for managing loop variables. |
| (#) | The foreach loop is used to iterate over the values of a list or collection variable. |
| [do...while loop](#) | 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 while, for, or do..while loops. |
* * *
## Loop Control Statements
Loop control statements change the code's execution order, allowing you to jump to other parts of the code.
Perl provides the following loop control statements:
| Control Statement | Description |
| --- | --- |
| (#) | Stops executing the statements from the next statement after `next` to the end of the loop body, then executes the continue block, and then returns to the beginning of the loop body to start the next iteration. |
| (#) | Exits the loop block, thereby ending the loop. |
| (#) | The continue block is usually executed before the condition is re-evaluated. |
| (#) | The redo statement jumps directly to the first line of the loop body to repeat the execution of the current loop. Statements after the redo statement are not executed, and the continue block is also not executed. |
| (#) | Perl has three forms of goto: `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 be used to create an infinite loop in the traditional sense.
Since none of the three expressions that make up the loop are required, you can leave some condition expressions blank to form an infinite loop.
## Example
#!/usr/bin/perl for( ; ; ){printf"Loop will execute infinitely.n"; }
> 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 generally, Perl programmers prefer to use the `for(;;)` structure to represent an infinite loop.
YouTip