Lua中的 if...else 语句
#Lua 中的 if…else 语句
如果 if 语句后面跟上 else 语句,那么条件为假时就执行 else 语句的代码。
##语法
Lua 语言中 if…else 语句的语法如下所示:
1 | if(boolean_expression) |
–[ 定义局部变量 –]
a = 100;
–[ 检查条件 –]
if( a < 20 )
then
–[ 如果条件为真,则输出如下内容 –]
print(“a is less than 20” )
else
–[ 如果条件为假,则输出如下内容 –]
print(“a is not less than 20” )
end
print(“value of a is :”, a)
1 |
|
a is not less than 20
value of a is : 100
1 |
|
if(boolean_expression 1)
then
–[ 如果布尔表达式 1 为真时,则执行此处代码。–]
else if( boolean_expression 2)
–[ 如果布尔表达式 2 为真时,则执行此处代码。 –]
else if( boolean_expression 3)
–[ 如果布尔表达式 3为真时,则执行此处代码。 –]
else
–[ 当上面所有布尔表达式条件都为假时执行此处代码。–]
end
1 |
|
–[ 定义局部变量 –]
a = 100
–[ 检查布尔条件 –]
if( a == 10 )
then
–[ 条件为真时输出如下内容 –]
print(“Value of a is 10” )
elseif( a == 20 )
then
–[ if else if 条件为真时 –]
print(“Value of a is 20” )
elseif( a == 30 )
then
–[ if else if 条件为真时 –]
print(“Value of a is 30” )
else
–[ 如果上述条件全部为假时 –]
print(“None of the values is matching” )
end
print(“Exact value of a is: “, a )
1 |
|
None of the values is matching
Exact value of a is: 100