📜  Perl-循环

📅  最后修改于: 2020-10-16 05:28:02             🧑  作者: Mango






Sr.No. Loop Type & Description
1 while loop

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

2 until loop

Repeats a statement or group of statements until a given condition becomes true. It tests the condition before executing the loop body.

3 for loop

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

4 foreach loop

The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn.

5 do…while loop

Like a while statement, except that it tests the condition at the end of the loop body

6 nested loops

You can use one or more loop inside any another while, for or do..while loop.




Sr.No. Control Statement & Description
1 next statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

2 last statement

Terminates the loop statement and transfers execution to the statement immediately following the loop.

3 continue statement

A continue BLOCK, it is always executed just before the conditional is about to be evaluated again.

4 redo statement

The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed.

5 goto statement

Perl supports a goto command with three forms: goto label, goto expr, and goto &name.


如果条件永远不会为假,则循环将变为无限循环。传统上, for循环用于此目的。由于形成for循环的三个表达式都不是必需的,因此可以通过将条件表达式保留为空来进行无限循环。

for( ; ; ) {
   printf "This loop will run forever.\n";

您可以通过按Ctrl + C键终止上述无限循环。