Lua流程控制

Lua流程控制

流程语句通过程序设定一个或多个条件语句来设定.在条件为true时执行指定程序代码,在条件为false执行其他代码


控制流程图

格式:

if(布尔表达式)
then
     --[ 在布尔表达式为 true 时执行的语句 ]--
end

在布尔表达式为true是会在if代码块中被执行,为false是,执行end之后的代码
Lua认为false和nil为假,true和非nil为真,

注意:lua中的0 为true

if...else 语句

语法格式:

if(布尔表达式)
then
     --[ 在布尔表达式为 true 时执行的语句 ]--
else
     --[ 在布尔表达式为 true 时执行的语句 ]--
end
流程图

为true时走if中的代码块会被执行,为false时,else的代码块被执行

if ... elseif ... else 语句

格式

if( 布尔表达式 1)
then
     --[ 在布尔表达式 1 为 true 时执行该语句块 ]--
elseif( 布尔表达式 2)
then
     --[ 在布尔表达式 2 为 true 时执行该语句块 ]--
elseif( 布尔表达式 3)
then
     --[ 在布尔表达式 3 为 true 时执行该语句块 ]--
else 
     --[ 如果以上布尔表达式都不为 true 则执行该语句块 ]--
end

嵌套语句

格式:

if(布尔表达式1)
then
     --[ 在布尔表达式 1 为 true 时执行该语句块 ]--
     if(布尔表达式2)
          --[ 在布尔表达式 2 为 true 时执行该语句块 ]--
     then
     end
end

你可能感兴趣的:(Lua流程控制)