当前位置: 移动技术网 > IT编程>脚本编程>Lua > 详解Lua中的if语句的使用方法

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

2017年12月08日  | 移动技术网IT编程  | 我要评论

 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


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网