当前位置:网站首页>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
边栏推荐
猜你喜欢

299. Number guessing game

Hyperbdr cloud disaster recovery v3 Release of version 3.0 | upgrade of disaster recovery function and optimization of resource group management function

volatile的含义以及用法

RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page

Force buckle-746 Climb stairs with minimum cost

Distinct use of spark operator

Day (6) of picking up matlab
JIRA screenshot

Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions

捡起MATLAB的第(7)天
随机推荐
Install redis and deploy redis high availability cluster
299. Number guessing game
【现代电子装联期末复习要点】
What does cloud disaster tolerance mean? What is the difference between cloud disaster tolerance and traditional disaster tolerance?
Cloud migration practice in the financial industry Ping An financial cloud integrates hypermotion cloud migration solution to provide migration services for customers in the financial industry
Win11 / 10 home edition disables the edge's private browsing function
[key points of final review of modern electronic assembly]
一文掌握vscode远程gdb调试
JSP learning 3
dlopen/dlsym/dlclose的简单用法
PS为图片添加纹理
js正則判斷域名或者IP的端口路徑是否正確
Win11/10家庭版禁用Edge的inprivate浏览功能
Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
Start Oracle service on Linux
Oracle data pump usage
第十天 异常机制
Research and Practice on business system migration of a government cloud project
捡起MATLAB的第(9)天
Spark 算子之coalesce与repartition