Lua学习之路(六) ------ Lua 流程控制(if)
Lua 编程语言流程控制语句通过程序设定一个或多个条件语句来设定。在条件为 true 时执行指定程序代码,在条件为 false 时执行其他指定代码。
以下是典型的流程控制流程图:
控制结构的条件表达式结果可以是任何值,Lua认为false和nil为假,true和非nil为真。
要注意的是Lua中 0 为 true:
--[ 0 为 true ] if(0) then print("0 为 true") end
以上代码输出结果为:
0 为 true
Lua 提供了以下控制结构语句:
语句 |
描述 |
if 语句 由一个布尔表达式作为条件判断,其后紧跟其他语句组成。
|
|
if 语句 可以与 else 语句搭配使用, 在 if 条件表达式为 false 时执行 else 语句代码。
|
|
你可以在if 或 else if中使用一个或多个 if 或 else if 语句 。
|
if语句
if(布尔表达式) then
为true时要执行的代码
end
if else语句
if(布尔表达式) then
为true时要执行的代码
else
为false时要执行的代码
end
if嵌套
if(布尔表达式1) then
布尔表达式1 为true时要执行的代码
else if(布尔表达式2) then (注意不要加空格)
布尔表达式2为true时要执行的代码
else
布尔表达式2也为false时要执行的代码
end