当前位置:网站首页>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
边栏推荐
- Detailed explanation of gzip and gunzip decompression parameters
- 第九天 static 抽象类 接口
- 299. Number guessing game
- JS regular determines whether the port path of the domain name or IP is correct
- R语言中实现作图对象排列的函数总结
- The system research problem that has plagued for many years has automatic collection tools, which are open source and free
- Distinct use of spark operator
- MySQL的btree索引和hash索引区别
- Findstr is not an internal or external command workaround
- ESXi封装网卡驱动
猜你喜欢

How can poor areas without networks have money to build networks?

Sort by character occurrence frequency 451

TIA博图——基本操作

You need to know about cloud disaster recovery

下载并安装MongoDB

What is the experience of using prophet, an open source research tool?

299. Number guessing game

Meaning and usage of volatile

Spark 算子之distinct使用

面试题 17.10. 主要元素
随机推荐
Force buckle-746 Climb stairs with minimum cost
如何进行应用安全测试(AST)
Sortby use of spark operator
Interview question 17.10 Main elements
Findstr is not an internal or external command workaround
捡起MATLAB的第(2)天
捡起MATLAB的第(7)天
The biggest winner is China Telecom. Why do people dislike China Mobile and China Unicom?
Day (3) of picking up matlab
Master vscode remote GDB debugging
Compile, connect -- Notes
C language self compiled string processing function - string segmentation, string filling, etc
Government cloud migration practice: Beiming digital division used hypermotion cloud migration products to implement the cloud migration project for a government unit, and completed the migration of n
JS regular détermine si le nom de domaine ou le chemin de port IP est correct
JSP learning 3
一文读懂串口及各种电平信号含义
Method 2 of drawing ROC curve in R language: proc package
linux上启动oracle服务
Day (9) of picking up matlab
Six scenarios of cloud migration