Perl loop

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

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

Loop statements allow us to execute a statement or group of statements more than once, and here's a flowchart of circular statements in most programming languages:

Perl loop

Note that the numbers 0, the strings '0', '", empty list(), and undef are false, and the other values are true. True uses ! before returns false.

The Perl language provides the following types of loops:

The type of loop Describe

While loop

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

The 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 to simplify the code that manages circular variables.

Foreach loop

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

do... While loop

In addition to the fact that it tests the condition at the end of the loop body, others are similar to while statements.

Nested loops

You can do in while, for, or do. One or more loops are used within the while loop.

Loop control statements

Loop control statements change the order in which code is executed, allowing you to jump code.

Perl provides the following circular control statements:

The control statement Describe

Next statement

Stop executing the statement between the next statement of the next statement of the next statement and the end of the loop body identifier, go to execute thecontinue statement block, and then return to the beginning of the loop body to start the next loop.

The last statement

Exit the loop statement block to end the loop

The continue statement

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

Redo statement

The redo statement goes directly to the first line of the loop body and begins to repeat 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 and NAME.

Unlimited loops

If the condition is never false, the loop becomes an infinite loop.

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

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

#!/usr/bin/perl
 
for( ; ; )
{
   printf "循环会无限执行。\n";
}

You can terminate the loop by pressing the Ctrl-C key.

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