4.8. Lua for cycle

发布时间 : 2025-10-25 13:34:11 UTC      

Page Views: 10 views

In Lua programming language the for loop statement can repeat the specified statement, and the number of times can be repeated in the for control in the statement.

In Lua programming language for , there are two main categories of statements:

  • Numerical value for cycle

  • Generics for cycle

4.8.1. Numerical for cycle #

Lua numerical value in programming language for circular syntax format:

for var=exp1,exp2,exp3 do <executor> end 

var from exp1 change to exp2 , with each change to exp3 increase step by step var and execute the “executive body” once. exp3 is optional, if not specified, defaults to 1.

4.8.2. Example #

Example #

for i=1,f(x) do print(i) end for i=10,1,-1 do print(i) end 

for are evaluated once before the start of the loop, and will not be evaluated later like the one above. f(x) is executed only once before the loop starts, and the result is used in later loops.

Verify as follows:

Example #

#!/usr/local/bin/lua function f(x) print("function") return x*2 end for i=1,f(5) do print(i) end 

The output result of the above example is:

function 1 2 3 4 5 6 7 8 9 10 

You can see the function. f(x) execute only once before the loop starts.

4.8.3. Generic for loop #

Generics for loop iterates through all values through an iterator function, similar to the one in java foreach statement.

Lua generics in programming languages for circular syntax format:

--Print all values of array a a = {"one", "two", "three"} for i, v in ipairs(a) do print(i, v) end 

i is an array index value v is the array element value of the corresponding index. ipairs is an iterator function provided by Lua to iterate through arrays.

4.8.4. Example #

Cyclic array days :

Example #

#!/usr/local/bin/lua days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"} for i,v in ipairs(days) do print(v) end 

The output result of the above example is:

Sunday Monday Tuesday Wednesday Thursday Friday Saturday 
《地理信息系统原理、技术与方法》  97

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