Lua学习六----------Lua流程控制

© 版权声明:本文为博主原创文章,转载请注明出处

Lua流程控制

  - 通过程序设定一个或多个条件语句

  - 在条件为true时执行指定程序代码,在条件为false时指定其他指定程序代码

  - 控制结构语句:if语句、if...else语句、if嵌套语句

1.if语句

  - 语法:if(布尔表达式) then ...<条件为true时执行语句> end

2.if...else语句 

  - 语法:if(布尔表达式) then ...<条件为true时执行语句> else ...<条件为false时执行语句> end

3.if嵌套语句

  - 在一个if或者else if语句中嵌入其他的if或else if语句

4.flowControl.lua

a = 10
if(a > 0) then						-- if语句
	print("a大于0")
end
print("a = ", a)

b = -1
if(b > 0) then						-- if...else语句
	b = b * 10
else
	b = -b * 10
end
print("b = ", b)

c = 0
if(c > 0) then						-- if...else if...else语句
	c = c + 2
else if(c == 0) then
	c = c + 1
else
	c = c - 1
end									-- 多一个else if就要多一个end
end
print("c = ", c)

d = -1
if(d > 0) then						-- if嵌套语句
	d = d + 10
else if (d == 0) then
	d = 10
else
	if(d > -10 and d < 0) then
		d = (d + 10) * 5
	else
		d = (d + 20) * 3
	end
end
end
print("d = ", d)

5.效果预览

参考:http://www.runoob.com/lua/lua-decision-making.html

原文地址:https://www.cnblogs.com/jinjiyese153/p/6835040.html