Lua学习--4
结构控制
2.4.4 – Control Structures
The control structures if, while, and repeat have the usual meaning and familiar syntax:
stat ::= while exp do block end stat ::= repeat block until exp stat ::= if exp then block {elseif exp then block} [else block] end
Lua also has a for statement, in two flavors (see §2.4.5).
The condition expression of a control structure can return any value. Both false and nil are considered false. All values different from nil and false are considered true (in particular, the number 0 and the empty string are also true).
In the repeat–until loop, the inner block does not end at the until keyword, but only after the condition. So, the condition can refer to local variables declared inside the loop block.
The return statement is used to return values from a function or a chunk (which is just a function). Functions and chunks can return more than one value, and so the syntax for the return statement is
stat ::= return [explist]
The break statement is used to terminate the execution of a while, repeat, or for loop, skipping to the next statement after the loop:
stat ::= break
A break ends the innermost enclosing loop.
The return and break statements can only be written as the last statement of a block. If it is really necessary to return or break in the middle of a block, then an explicit inner block can be used, as in the idioms do return end
and do break end
, because now return and break are the last statements in their (inner) blocks.
在结构控制中有循环 repeat block until exp do block while exp if exp then block {elseif exp then block} {else block} end
for v=exp1,to_exp2{,step_exp3} do block end
exp 表达式只有 false 和 nil 才会是false,其它值都是true(包括数值的0和字符串的空)
在循环block 中 可以通过 break 结束当前block最内层循环 或goto 跳转 到 label,格式如下:
:: Label ::
数值型for循环
For 语法 do local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3) if not (var and limit and step) then error() end while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do local v = var block var = var + step end end
Note the following:
- All three control expressions are evaluated only once, before the loop starts. They must all result in numbers.
var
,limit
, andstep
are invisible variables. The names shown here are for explanatory purposes only.- If the third expression (the step) is absent, then a step of 1 is used.
- You can use break to exit a for loop.
- The loop variable
v
is local to the loop; you cannot use its value after the for ends or is broken. If you need this value, assign it to another variable before breaking or exiting the loop.
针对上面的for 结构语法 做如下说明: 1、所有的表达式只在循环开始之前 计算一次,并且所有返回结果必须是数值型 2、如果步长表达式省略,默认为1 3、break 可以中止block的正常执行而退出最近的一级for循环 4、结构 中的 值 v 是for循环内部变量,循环体之外无效
泛型for循环
The generic for statement works over functions, called iterators. On each iteration, the iterator function is called to produce a new value, stopping when this new value is nil. The generic for loop has the following syntax:
stat ::= for namelist in explist do block end namelist ::= Name {`,´ Name}
A for statement like
for var_1, ···, var_n in explist do block end
is equivalent to the code:
do local f, s, var = explist while true do local var_1, ···, var_n = f(s, var) var = var_1 if var == nil then break end block end end
Note the following:
explist
is evaluated only once. Its results are an iterator function, a state, and an initial value for the first iterator variable.f
,s
, andvar
are invisible variables. The names are here for explanatory purposes only.- You can use break to exit a for loop.
- The loop variables
var_i
are local to the loop; you cannot use their values after the for ends. If you need these values, then assign them to other variables before breaking or exiting the loop.
泛型循环,依赖于迭代器,每执行一次block 迭代器都会产生一个新的值,如果值为nil则循环结束 泛型 for 循环通过一个迭代器函数来遍历所有值,类似 java 中的 foreach 语句。 Lua 编程语言中泛型 for 循环语法格式: --打印数组a的所有值 a = {"one", "two", "three"} for i, v in ipairs(a) do print(i, v) end i是数组索引值,v是对应索引的数组元素值。ipairs是Lua提供的一个迭代器函数,用来迭代数组。
c1,d1=3 --print(c1,d1) a,b=1,2 if( a>b ) then print('a='..a) elseif(a ~= b ) then print('b='..b) else print(' a equal b') end
i=5 repeat print(i) i=i-1 until i<=0