详解Lua中if ... else语句的使用方法
技术  /  管理员 发布于 5年前   218
if 语句后面可以跟一个可选的else语句,当布尔表达式为假该语句执行。
语法
在Lua编程语言中的if ... else语句的语法是:
复制代码 代码如下:if(boolean_expression)
then
--[ statement(s) will execute if the boolean expression is true --]
else
--[ statement(s) will execute if the boolean expression is false --]
end
如果布尔表达式的值为true,那么if代码块将被执行,否则else代码块将被执行。
Lua程序设计语言假定布尔true和非零值的任意组合作为true,以及它是否是布尔假或零,则假定为false值。但应当注意的是,在Lua零值被视为true。
例如:
复制代码 代码如下:--[ local variable definition --]
a = 100;
--[ check the boolean condition --]
if( a < 20 )
then
--[ if condition is true then print the following --]
print("a is less than 20" )
else
--[ if condition is false then print the following --]
print("a is not less than 20" )
end
print("value of a is :", a)
当建立和运行上面的代码,它会产生以下结果。
复制代码 代码如下:a is not less than 20
value of a is : 100
if...else if...else 语句
if语句后面可以跟一个可选的else if ... else语句,这是非常有用的使用,以测试各种条件单个if...else if 语句。
当使用if , else if , else语句有几点要记住使用:
语法
if...else if...else...else语句在Lua编程语言的语法是:
复制代码 代码如下:if(boolean_expression 1)
then
--[ Executes when the boolean expression 1 is true --]
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
例如:
复制代码 代码如下:--[ local variable definition --]
a = 100
--[ 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 )
当建立和运行上面的代码,它会产生以下结果。
复制代码 代码如下:None of the values is matching
Exact value of a is: 100
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号