Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Loops

Perl Loops

Sometimes we may need to execute the same block of code multiple times. Normally, statements are executed sequentially: the first statement in a function executes first, followed by 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. The following is a flowchart of a loop statement in most programming languages:

Loop Structures

Note that the number 0, the string '0', "", the empty list (), and undef are false , all other values are true. Prefixing true with ! or notreturns false.

The Perl language provides the following loop types:

Loop types Description

while loop

When the given condition is true, a statement or group of statements is executed repeatedly. The condition is tested before the loop body executes.

until loop

Repeat a statement or group of statements until the given condition is true. The condition is tested before the loop body executes.

for loop

Executes a sequence of statements multiple times, simplifying the code for managing 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 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 statements

Loop control statements change the execution order of code, allowing you to jump within the code.

Perl provides the following loop control statements:

Control statement Description

next statement

Stop executing the statements from the one after the next statement to the end-of-loop-body marker, switch to executing the continue block, and then return to the beginning of the loop body to start the next iteration.

last statement

Exit the loop statement block, thereby ending the loop

continue statement

The continue block is usually executed before the condition is evaluated again.

redo statement

The redo statement goes directly to the first line of the loop body to repeat the current iteration; statements after the redo statement are no longer executed, and the continue block is no longer executed;

goto statement

Perl has three forms of goto: got LABLE, goto EXPR, and goto &NAME.

Infinite loop

If the condition is never 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 make up a loop is required, you can leave some conditional expressions empty to form an infinite loop.

Example

#!/usr/bin/perl

for

(

; ;

)

{

printf

"

The loop will execute infinitely.

\n

"

;

}

You can press Ctrl + C to terminate the loop.

When the conditional 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.