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

1.循环语句

1.1 while

--while 语句练习

m_table = {1, 2, 3}
local i = 1

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

运行结果

1
2
3

2.2 repeat

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

--repeat 语句练习

local snum = 1

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

运行结果

1
2
3
4
5
6
7
8
9

2.3 for 语句

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

运行结果

1
3
5
7
9

2.逻辑关系运算符

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

运行结果

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

运行结果

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

运行结果

true
true
false

猜你喜欢

转载自blog.csdn.net/su749520/article/details/80699755