Perl loop

Perl loop

we may need to execute the same piece of code multiple times. In general, statements are executed in order: the first statement in the function is executed first, then the second statement, and so on.

Programming languages provide a variety of control structures for more complex execution paths.

Looping statements allow us to execute a single statement or group of statements.

The Perl language provides the following types of loops:

Cycle TypeDescription

while loop

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

until loop

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

for loop

Execute a sequence of statements multiple times, simplifying the code that manages loop variables.

foreach loop

The foreach loop is used to iterate over the value of a list or collection variable.

do...while loop

is similar to the while statement except that it is a test condition at the end of the loop body.

nested loops

You can use one or more loops within a while, for, or do..while loop.

loop control statement

The loop control statement changes the order in which the code is executed, through which you can implement code jumps.

Perl provides the following loop control statements:

Control StatementDescription

next statement

Stop executing the statement from the next statement of the next statement to the end of the loop body identifier, go to the execution of the continue statement block, and then return to the beginning of the loop body to start the next loop.

last statement

Exit the loop statement block to end the loop

continue statement

The continue statement block is usually executed before the conditional statement is judged again.

redo statement

The redo statement directly goes to the first line of the loop body and repeats the loop. The statement after the redo statement is no longer executed, and the continue statement block is no longer executed.

goto statement

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

Infinite loop

If the condition is never false, the loop will become an infinite loop.

The for loop can be used to implement an infinite loop in the traditional sense.

Because 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/container/perl for( ; ) printf "The circle will execute indefinitely.n";

You can press Ctrl + C to terminate the loop.

When a conditional expression does not exist, it is assumed to be true . You can also set an initial value and an incremental expression, but in general, Perl programmers prefer to use the for(;;) structure to represent an infinite loop.

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.