当前位置:网站首页>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
边栏推荐
- Read the meaning of serial port and various level signals
- MySQL - execution process of MySQL query statement
- VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
- 运维流程有多重要,听说一年能省下200万?
- Day (2) of picking up matlab
- Questions about disaster recovery? Click here
- Win11/10家庭版禁用Edge的inprivate浏览功能
- matplotlib教程05---操作图像
- Day (6) of picking up matlab
- There is a problem with the light switch from 1 to 100
猜你喜欢
How can poor areas without networks have money to build networks?
Force buckle - 198 raid homes and plunder houses
一文掌握vscode远程gdb调试
Jour (9) de ramassage de MATLAB
Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
C language self compiled string processing function - string segmentation, string filling, etc
捡起MATLAB的第(8)天
运维流程有多重要,听说一年能省下200万?
Ice -- source code analysis
Implement default page
随机推荐
Ice -- source code analysis
通过Feign在服务之间传递header请求头信息
Meaning and usage of volatile
Spark 算子之distinct使用
Hypermotion cloud migration helped China Unicom. Qingyun completed the cloud project of a central enterprise and accelerated the cloud process of the group's core business system
Solution to the fourth "intelligence Cup" National College Students' IT skills competition (group B of the final)
Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
基于GPU实例的Nanopore数据预处理
homwbrew安装、常用命令以及安装路径
Oak-d raspberry pie cloud project [with detailed code]
Spark 算子之filter使用
最详细的背包问题!!!
ESP32_ Arduino
JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory
Unity shader learning
Nanny Anaconda installation tutorial
Method 2 of drawing ROC curve in R language: proc package
GRBL学习(一)
[section 5 if and for]
捡起MATLAB的第(7)天