结构控制
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 exp do block end
exp 表达式只有 false 和 nil 才会是false,其它值都是true(包括数值的0和字符串的空)
在循环block 中 可以通过 break 结束当前block最内层循环 或goto 跳转 到 label,格式如下:
:: Label ::
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
标签:break,repeat,return,--,学习,Lua,exp,end,block From: https://www.cnblogs.com/hztech/p/17179426.html