Perl cycle


Release date:2023-10-14 Update date:2023-10-21 Editor:admin View counts:239

Label:

Perl cycle

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 is executed 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 multiple times. Here is a flowchart of loop statements in most programming languages:

Image0

Note that the number 0, string '0'"" , empty list() , and undef for false all other values are true . true previous use ! or not then return to false .

Perl language provides the following loop types:

Cycle type

Description

While cycle

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

Until cycle

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

For cycle

Execute a sequence of statements multiple times to simplify the code for managing loop variables.

Foreach cycle

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

Do…while cycle

Except that it tests the condition at the end of the loop body, the rest is similar to the while statement.

Nested loop

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

Loop control statement

Loop control statements change the order in which the code is executed, through which you can jump the code.

Perl following loop control statements are provided:

Control statement

Description

Next statement

Stop executing the statement from the next statement of the next statement to the identifier at the end of the loop body, go to execute 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, thus ending the loop

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 to repeat the execution of 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 forms of goto: got LABLE,goto EXPR, and goto & NAME.

Infinite cycle

If the conditions will never be false loop becomes an infinite loop

for loops can be used to realize infinite loops in the traditional sense.

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

Example

#!/usr/bin/perlfor(; ;){printf"The loop will execute infinitely.\\n";}

You can press Ctrl + C to end 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 tend to use the for(;;) structure to represent an infinite loop.

Powered by TorCMS (https://github.com/bukun/TorCMS).