天天看点

06 Lua基础-循环语句和逻辑运算关键字

1.循环语句

1.1 while

--while 语句练习

m_table = {, , }
local i = 

while m_table[i] do
    print(m_table[i])
    i = i + 
end 
           

运行结果

1
2
3
           

2.2 repeat

repeat 相当于其他语言的 do-while

--repeat 语句练习

local snum = 

repeat
    print(snum)
    snum = snum + 
until snum == 
           

运行结果

1
2
3
4
5
6
7
8
9
           

2.3 for 语句

--for 语句
-- 初始值,阈值,步进值
for i=, ,  do
    print(i)
end
           

运行结果

1
3
5
7
9
           

2.逻辑关系运算符

  1. and
--and 如果第一为真,则返回第二个数
print( and )
print( and )
-- 如果第一为假,则发你第一个数,lua只有 false 和 nil 表示假
print(false and )
print(nil and )
           

运行结果

false
nil
           
  1. or
--or 如果第一为z真,则返回第一个个数
print( or )
print(  or )
-- 如果第一为假,则发你第二个数,lua只有 false 和 nil 表示假
print(false or )
print(nil or )
           

运行结果

1
0
5
5
           
  1. not
--not
print(not false)
print(not nil)
print(not )
           

运行结果

true
true
false
           

继续阅读