当前位置:网站首页>05 Lua 控制结构
05 Lua 控制结构
2022-04-23 16:13:00 【陈皮的JavaLib】
1 判断语句
判断语句块由 if,elseif,else 关键字组成,并且 以 end 关键字结尾。条件表达式的结果可以是任何值。Lua 中将 false 和 nil 当作假,其他值都作为真(0和空字符串也当作真)。
-- 定义1
if 表达式 then
语句块
end
-- 定义2
if 表达式 then
语句块
else
语句块
end
-- 定义3
if 表达式 then
语句块
elseif 表达式 then
语句块
elseif 表达式 then
语句块
...
else
语句块
end
定义1演示例子如下:
a = 10
if a < 20 then print("ok") end
if nil then print("I am nil") end
if 0 then print("I am 0") end
function func(b)
if a == b then
print("equals")
end
end
func(10)
-- 输出结果如下
ok
I am 0
equals
定义2演示例子如下:
name = "chenpi"
function func(myname)
if name == myname then
print("equals")
else
print("not equals")
end
end
func("chenpiok")
-- 输出结果如下
not equals
定义3演示例子如下:
function func(age)
if age ==10 then
print("1")
elseif age == 20 then
print("2")
else
print("3")
end
end
func(20) -- 2
2 循环语句
2.1 while…do
while 循环条件 do
循环体
end
-- 例子,输出1-5五个数字
local i = 1
while i <= 5 do
print(i)
i = i + 1
end
2.2 for…do
for 变量 = 初始值, 终止值, 步长 do
循环体
end
-- 例子,输出1-5五个数字
for i = 1, 5, 1 do
print(i)
end
for i = 1, 5 do -- 步长可以省略,默认为1
print(i)
end
-- 输出5-1
for i = 5, 1, -1 do
print(i)
end
需要注意,初始值,终止值,步长都只会被执行一次,所以即使你在循环过程中,改变它们的值也没用。
-- 以下例子还是只输出1-5五个数字
local x = 5
local y = 1
for i = 1, x, y do
print(i)
x = 10
y = 3
end
2.3 repeat…until
repeat
循环体
until 循环条件
-- 例子,输出1-5五个数字
local i = 1
repeat
print(i)
i = i + 1
until i > 5
2.4 for…in…do
for 变量列表 in 迭代器 do
循环体
end
t = {
"a", "b", "c"}
for k,v in pairs(t) do
print(k..":"..v)
end
-- 输出结果如下
1:a
2:b
3:c
我们可以自定义一个迭代器。
-- 定义迭代器
function mypairs(t)
return myfunc, t, 0 -- 返回迭代函数,待遍历的集合,控制变量。待遍历的集合,控制变量会传入迭代函数中
end
function myfunc(t, i)
i = i + 1
local v = t[i]
if v then
return i, v
end
return nil, nil
end
t = {
"a", "b", "c"}
for k,v in mypairs(t) do
print(k..":"..v)
end
-- 输出结果如下
1:a
2:b
3:c
2.5 break
break 可以跳出循环。
for i = 1, 5, 1 do
print(i)
if i == 3 then
break
end
end
本次分享到此结束啦~~
我是陈皮,一个在互联网 Coding 的 ITer。如果觉得文章对你有帮助,点赞、收藏、关注、评论,您的支持就是我创作最大的动力!
版权声明
本文为[陈皮的JavaLib]所创,转载请带上原文链接,感谢
https://javalib.blog.csdn.net/article/details/124335329
边栏推荐
- 保姆级Anaconda安装教程
- 一文掌握vscode远程gdb调试
- What is cloud migration? The four modes of cloud migration are?
- js正則判斷域名或者IP的端口路徑是否正確
- C, calculation method and source program of bell number
- JSP learning 3
- Unity Shader学习
- Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
- 力扣-198.打家劫舍
- Filter usage of spark operator
猜你喜欢
第九天 static 抽象类 接口
Master vscode remote GDB debugging
Vision of building interstellar computing network
Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
Day (6) of picking up matlab
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
How can poor areas without networks have money to build networks?
Day (8) of picking up matlab
The biggest winner is China Telecom. Why do people dislike China Mobile and China Unicom?
MySQL - execution process of MySQL query statement
随机推荐
Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function
Six scenarios of cloud migration
下载并安装MongoDB
面试题 17.10. 主要元素
Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification
The most detailed knapsack problem!!!
Findstr is not an internal or external command workaround
TIA博图——基本操作
linux上启动oracle服务
Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
R语言中绘制ROC曲线方法二:pROC包
Spark 算子之filter使用
Leetcode-374 guess the size of the number
力扣-746.使用最小花费爬楼梯
第十天 异常机制
运维流程有多重要,听说一年能省下200万?
Postman batch production body information (realize batch modification of data)
ESP32_ Arduino
JSP learning 3
捡起MATLAB的第(7)天