if 语句后面可以跟一个可选的else语句,当布尔表达式为假该语句执行。
语法
在lua编程语言中的if ... else语句的语法是:
如果布尔表达式的值为true,那么if代码块将被执行,否则else代码块将被执行。
lua程序设计语言假定布尔true和非零值的任意组合作为true,以及它是否是布尔假或零,则假定为false值。但应当注意的是,在lua零值被视为true。
例如:
当建立和运行上面的代码,它会产生以下结果。
if...else if...else 语句
if语句后面可以跟一个可选的else if ... else语句,这是非常有用的使用,以测试各种条件单个if...else if 语句。
当使用if , else if , else语句有几点要记住使用:
语法
if...else if...else...else语句在lua编程语言的语法是:
else if( boolean_expression 2)
--[ executes when the boolean expression 2 is true --]
else if( boolean_expression 3)
--[ executes when the boolean expression 3 is true --]
else
--[ executes when the none of the above condition is true --]
end
例如:
--[ check the boolean condition --]
if( a == 10 )
then
--[ if condition is true then print the following --]
print("value of a is 10" )
elseif( a == 20 )
then
--[ if else if condition is true --]
print("value of a is 20" )
elseif( a == 30 )
then
--[ if else if condition is true --]
print("value of a is 30" )
else
--[ if none of the conditions is true --]
print("none of the values is matching" )
end
print("exact value of a is: ", a )
当建立和运行上面的代码,它会产生以下结果。
如对本文有疑问, 点击进行留言回复!!
网友评论