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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值