Lua if ... else statement
if ... else statement
Lua if statement and else statement with the use of a block of code executed else the statement is false if the conditional expression.
Lua if ... else statement syntax is as follows:
if(布尔表达式) then --[ 布尔表达式为 true 时执行该语句块 --] else --[ 布尔表达式为 false 时执行该语句块 --] end
It will be executed when the will is true if the block of code in a Boolean expression when false, else block of code is executed in a Boolean expression.
Lua considered false and nil as false, true and non-nil is true. It should be noted Lua 0 is true.
if statement is a flow chart is as follows:
Examples
The following examples are used to determine the value of a variable:
--[ 定义变量 --] a = 100; --[ 检查条件 --] if( a < 20 ) then --[ if 条件为 true 时执行该语句块 --] print("a 小于 20" ) else --[ if 条件为 false 时执行该语句块 --] print("a 大于 20" ) end print("a 的值为 :", a)
The above code is executed as follows:
a 大于 20 a 的值为 : 100
if ... elseif ... else statement
Lua if and elseif ... else statement can be used with the statement, execution is false elseif conditional expression in if ... else statement block for detecting a plurality of conditional statements.
Lua if ... elseif ... else statement syntax is as follows:
if( 布尔表达式 1) then --[ 在布尔表达式 1 为 true 时执行该语句块 --] elseif( 布尔表达式 2) then --[ 在布尔表达式 2 为 true 时执行该语句块 --] elseif( 布尔表达式 3) then --[ 在布尔表达式 3 为 true 时执行该语句块 --] else --[ 如果以上布尔表达式都不为 true 则执行该语句块 --] end
Examples
The following examples of variable a value judgment:
--[ 定义变量 --] a = 100 --[ 检查布尔条件 --] if( a == 10 ) then --[ 如果条件为 true 打印以下信息 --] print("a 的值为 10" ) elseif( a == 20 ) then --[ if else if 条件为 true 时打印以下信息 --] print("a 的值为 20" ) elseif( a == 30 ) then --[ if else if condition 条件为 true 时打印以下信息 --] print("a 的值为 30" ) else --[ 以上条件语句没有一个为 true 时打印以下信息 --] print("没有匹配 a 的值" ) end print("a 的真实值为: ", a )
The above code is executed as follows:
没有匹配 a 的值 a 的真实值为: 100