5.18. Perl cycle

发布时间 : 2025-10-25 13:33:01 UTC      

Page Views: 10 views

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.

5.18.1. 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.

5.18.2. 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.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。