Perl loop

Perl loop

Sometimes, we may need to execute the same block of code multiple times. In general, statements are executed sequentially: the first statement in the function is executed first, followed by the second statement, and so on.

Programming languages ​​provide various control structures for more complex execution paths.

A loop statement allows us to execute a statement or a group of statements multiple times. Below is a flow chart of a loop statement in most programming languages:


Note that the number 0, the string '0' , "" , the empty list() , and undef are  false  , all other values ​​are  true . true is preceded by  !  or  not to return false.

The Perl language provides the following loop types:

Loop type describe

while loop

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

until loop

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

for loop

Executes 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 loop

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

loop control statement

The loop control statement changes the execution order of the code, through which you can realize the jump of the code.

Perl provides the following loop control statements:

control statement describe

next statement

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

last statement

Exit the loop block, thus ending the loop

continue statement

The continue 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 to execute the loop repeatedly. The statements after the redo statement are 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 becomes an infinite loop.

For loops can be used to implement infinite loops in the traditional sense.

Since none of the three expressions that make up the loop are required, you can leave some conditional expressions blank to form an infinite loop.

#!/usr/bin/perl
 
for( ; ; )
{
   printf "The loop will execute indefinitely.\n";
}

You can press Ctrl + C to terminate the loop.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326053852&siteId=291194637