发布于 2015-07-26 14:41:47 | 270 次阅读 | 评论: 0 | 来源: 网络整理

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)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务