详解Lua中if ... else语句的使用方法
“resuyu”通过精心收集,向本站投稿了3篇详解Lua中if ... else语句的使用方法,下面是小编为大家准备的详解Lua中if ... else语句的使用方法,欢迎阅读借鉴。
篇1:详解Lua中的if语句的使用方法
这篇文章主要介绍了详解Lua中的if语句的使用方法,是Lua入门学习中的基础知识,需要的朋友可以参考下
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
篇2:详解Lua中if ... else语句的使用方法
这篇文章主要介绍了详解Lua中if ... else语句的使用方法,是Lua入门学习中的基础知识,需要的朋友可以参考下
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 ,但必须在elseif之前。
if 之后可以有零到很多else if在else之前,
一旦一个else if成功,其它的elseif将不会被测试。
语法
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
篇3:详解Lua中repeat...until循环语句的使用方法
这篇文章主要介绍了详解Lua中repeat...until循环语句的使用方法,需要的朋友可以参考下
与for和while循环不同,在循环的顶部测试循环条件,Lua编程语言的repeat...until 循环检查循环底部的状态,
repeat...until 循环类似于while循环,不同的是do ... while循环是保证至少执行一次。
语法
Lua编程语言repeat...until循环的语法是:
代码如下:
repeat
statement(s)
until( condition )
注意的是,条件表达式出现在循环结束,所以在循环语句(S)执行一次之前的条件进行测试。
如果条件为false,控制流程跳转备份执行循环语句(S)再次执行。这个过程反复进行,直到给定的条件变为真。
流程图:
例如:
代码如下:
--[ local variable definition --]
a = 10
--[ repeat loop execution --]
repeat
print(“value of a:”, a)
a = a + 1
until( a >15 )
当建立并执行上述程序,它会产生以下结果:
代码如下:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
【详解Lua中if ... else语句的使用方法】相关文章:
3.错题本的使用方法
6.英语语法详解
7.论语中励志的语句
10.送给中考生的励志语句






文档为doc格式