详解Lua中的if语句的使用方法

2023-12-05 0 377

if语句由一个或多个语句组成一个布尔表达式。
语法

Lua编程语言的if语句语法是:

复制代码 代码如下:if(boolean_expression)
then
–[ statement(s) will execute if the boolean expression is true –]
end

如果布尔表达式的计算结果为代码的if语句为true,那么块将被执行。如果if语句的末尾(右大括号后)布尔表达式计算为false,那么第一组代码将被执行。

Lua程序设计语言假定布尔true和非零值的任意组合作为true,以及它是否是布尔假或零,则假定为false值。但应当注意的是,在Lua零值也会被视为true。

示例:

复制代码 代码如下:–[ local variable definition –]
a = 10;

–[ check the boolean condition using if statement –]
if( a < 20 )
then
–[ if condition is true then print the following –]
print(\”a is less than 20\” );
end
print(\”value of a is :\”, a);

当建立和运行上面代码,它会产生以下结果。

复制代码 代码如下:a is less than 20
value of a is : 10

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Lua 详解Lua中的if语句的使用方法 https://www.u-9.cn/jiaoben/lua/101334.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务