当前位置:网站首页>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
边栏推荐
- New developments: new trends in cooperation between smartmesh and meshbox
- Jour (9) de ramassage de MATLAB
- 捡起MATLAB的第(4)天
- There is a problem with the light switch from 1 to 100
- Database dbvisualizer Pro reported file error, resulting in data connection failure
- Win11/10家庭版禁用Edge的inprivate浏览功能
- Day (8) of picking up matlab
- Day (2) of picking up matlab
- MySQL的btree索引和hash索引区别
- VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
猜你喜欢
What is cloud migration? The four modes of cloud migration are?
Day (9) of picking up matlab
一文掌握vscode远程gdb调试
Homewbrew installation, common commands and installation path
Sortby use of spark operator
volatile的含义以及用法
Config learning notes component
TIA博图——基本操作
Master vscode remote GDB debugging
The system research problem that has plagued for many years has automatic collection tools, which are open source and free
随机推荐
如何进行应用安全测试(AST)
捡起MATLAB的第(2)天
基于GPU实例的Nanopore数据预处理
Coalesce and repartition of spark operators
PS为图片添加纹理
R语言中绘制ROC曲线方法二:pROC包
力扣-198.打家劫舍
Algorithem_ ReverseLinkedList
GRBL学习(一)
捡起MATLAB的第(5)天
Interview question 17.10 Main elements
TIA botu - basic operation
JS regular determines whether the port path of the domain name or IP is correct
PHP 零基础入门笔记(13):数组相关函数
There is a problem with the light switch from 1 to 100
Research and Practice on business system migration of a government cloud project
Force buckle-746 Climb stairs with minimum cost
捡起MATLAB的第(6)天
Read the meaning of serial port and various level signals
Spark 算子之distinct使用