当前位置:网站首页>lua 循环语句
lua 循环语句
2022-06-12 22:42:00 【o_瓜田李下_o】
lua 循环语句
while 循环
语法格式
while(condition)
do
statements
end
示例
# 循环脚本test.lua
[email protected] test % cat test.lua
i=0
while(i<=10) do
print(i)
i=i+1
end
# 执行循环脚本
[email protected] test % lua test.lua
0
1
2
3
4
5
6
7
8
9
10
for 循环
语法格式
# 数值for循环
# var 从 exp1 变化到 exp2,每次变化以 exp3 为步长递增,exp3可选(如果不设置,默认为1)
for var=exp1,exp2,exp3 do
<执行体>
end
# 范型for循环
a = {"one", "two", "three"}
for i, v in ipairs(a) do
print(i, v)
end
示例:数值for循环
# 计算0到10相加的结果
[email protected] test % cat test2.lua
sum=0
for i=0,10,1 do
sum=sum+i
end
print(sum)
# 执行脚本
[email protected] test % lua test2.lua
55
示例:范型for循环
# 遍历输出table中的元素
[email protected] test % cat test3.lua
t={1,2,3,4,5,6}
for key,value in pairs(t) do
print(key,value)
end
# 执行脚本
[email protected] test % lua test3.lua
1 1
2 2
3 3
4 4
5 5
6 6
until 循环
语法格式
# condition为true时退出循环
repeat
statements
until( condition )
示例
# 循环输出0到10之间的数字
[email protected] test % cat test4.lua
i=0
repeat
print(i)
i=i+1
until(i>10)
# 执行脚本
[email protected] test % lua test4.lua
0
1
2
3
4
5
6
7
8
9
10
break 语句
break:满足条件时,退出循环语句
# 满足i>10时,退出循环
[email protected] test % cat test5.lua
i=0
while(true) do
print(i)
i=i+1
if(i>10) then
break
end
end
# 执行脚本
[email protected] test % lua test5.lua
0
1
2
3
4
5
6
7
8
9
10
goto 语句
goto:跳转到标签处
# label格式
:: Label :: ==> Label为标签的名字,可自定义
示例:利用goto实现循环
# 循环输出1到10之间的数字
[email protected] test % cat test6.lua
i=0
::print:: print(i)
while(i<10) do
i=i+1
goto print
end
# 执行脚本
[email protected]o test % lua test6.lua
0
1
2
3
4
5
6
7
8
9
10
示例:goto标签语句块
# goto标签设置为语句块
[email protected] test % cat test7.lua
i=0
::block:: do
print(i)
end
if(i<10) then
i=i+1
goto block
else
print("end")
end
# 执行脚本
[email protected] test % lua test7.lua
0
1
2
3
4
5
6
7
8
9
10
end
边栏推荐
- China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
- Common rendering pipeline grooming
- C#读取word中表格数据
- VIM use the lower right 4 keys
- China Aquatic Fitness equipment market trend report, technical innovation and market forecast
- Su embedded training day13 - file IO
- MySQL case when then function use
- C language: how to give an alias to a global variable?
- 【数据分析】基于 kmeans实现数据聚类分组含Matlab源码
- JVM foundation > CMS garbage collector
猜你喜欢

Wechat applet withdrawal function

80 lines of code to realize simple rxjs
![[Part 8] semaphore source code analysis and application details [key points]](/img/e2/05c08435d60564aaa1172d2d574675.jpg)
[Part 8] semaphore source code analysis and application details [key points]

Photoshop:ps how to enlarge a picture without blurring

Audio and video technology development weekly 𞓜 234

SQL query list all views in SQL Server 2005 database - SQL query to list all views in an SQL Server 2005 database

设计消息队列存储消息数据的 MySQL 表格

Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation

C language: how to give an alias to a global variable?

be careful! Your Navicat may have been poisoned
随机推荐
[data analysis] data clustering and grouping based on kmeans, including Matlab source code
LeetCode —— 26. Remove duplicates from an ordered array
Database system composition
接口测试工具apipost3.0版本对于流程测试和引用参数变量
Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
Qt Quick 3D学习:使用鼠标键盘控制节点位置和方向
C # reading table data in word
Flutter series part: detailed explanation of GridView layout commonly used in flutter
China's elastic belt market trend report, technical dynamic innovation and market forecast
C语言:如何给全局变量起一个别名?
Configuring Dingding notification of SQL audit platform archery
【LeetCode】5. Longest Palindromic Substring
Zabbix的功能介绍和常用术语
flutter系列之:flutter中常用的GridView layout详解
[proteus simulation] simple digital tube timer clock
Implementation of master-slave replication and master-master replication for MySQL and MariaDB databases
JS fighting on...
How to specify your webpage's language so Google Chrome doesn't offer to translate it
Dolphin-2.0.3 cluster deployment document
iShot