Latest web development tutorials

Perl cycle

Sometimes, we may need to repeatedly perform the same piece of code. Under normal circumstances, the statements are executed sequentially: the first statement in the function executed first, followed by a second statement, and so on.

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

Loops allow us to repeatedly execute a statement or group of statements, the following is the most programming languages ​​loop flow chart?:

Loop structure

Note that the number 0, the string '0', "", an empty list (), and undef isfalse, other values are true.Use fronttrue! or notit returns false.

Perl language provides the following cycle types:

Type of cycle description

while loop

When a given condition is true, the statement or group of statements repeatedly performed. The test conditions will be executed before the body of the loop.

until loop

Repeat the statement or group of statements until a given condition is true. The test conditions will be executed before the body of the loop.

for loop

Repeatedly execute a sequence of statements, simplify code management loop variable.

foreach loop

foreach loop is used to iterate a list or a set of values ​​of the variables.

do ... while loop

Except that it is in the body of the loop at the end of the test conditions, while other similar statements.

Nested loop

You can use one or more of the loop in the while, for or do..while loop.

Loop control statements

Loop control statements change the order of execution of the code, through which you can jump code.

Perl provides the following loop control statements:

Control statements description

next statement

Starting next stop execution statement next statement to loop between the end of the statement identifier, turn to continue the implementation of the statement block and then back to the beginning of the loop body begin the next cycle.

last statement

Exit the loop statement block, thus ending the cycle

continue Statement

continue statement block is usually performed before the judge again conditional statements.

redo statement

redo statements directly to the first line of the loop body begin repeating this cycle, the statements are not executed redo statement, continue statement 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.

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

Since the three expressions constitute any one cycle is not required, you can be certain conditional expression blank to form an infinite loop.

for( ; ; )
   printf "循环会无限执行。\n";

You can press Ctrl + C keys to stop.

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