元素码农
基础
UML建模
数据结构
算法
设计模式
网络
TCP/IP协议
HTTPS安全机制
WebSocket实时通信
数据库
sqlite
postgresql
clickhouse
后端
rust
go
java
php
mysql
redis
mongodb
etcd
nats
zincsearch
前端
浏览器
javascript
typescript
vue3
react
游戏
unity
unreal
C++
C#
Lua
App
android
ios
flutter
react-native
安全
Web安全
测试
软件测试
自动化测试 - Playwright
人工智能
Python
langChain
langGraph
运维
linux
docker
工具
git
svn
🌞
🌙
目录
▶
Lua语言基础
▶
环境搭建
安装Lua解释器
配置开发环境
第一个Lua程序
▶
基本语法
变量与数据类型
运算符与表达式
控制结构
▶
数据结构
表(Table)详解
数组与迭代
字符串处理
▶
Lua高级编程
▶
函数编程
函数定义与调用
闭包与作用域
高阶函数应用
▶
元表与元方法
元表基础
操作符重载
继承与对象系统
▶
协程编程
协程基础
生产者-消费者模式
协程调度实践
▶
Lua应用实践
▶
游戏开发
Lua与游戏引擎集成
AI脚本实现
热更新机制
▶
系统编程
Lua与C交互
扩展库开发
性能优化技巧
▶
实用工具开发
配置文件解析
自动化测试框架
网络编程基础
发布时间:
2025-03-24 12:04
↑
☰
# Lua控制结构 本文将介绍Lua中的控制结构,包括条件语句、循环语句等,帮助你理解如何控制程序的执行流程。 ## 条件语句 ### if语句 Lua提供了if-then-else结构进行条件判断: ```lua local score = 85 -- 简单if语句 if score >= 60 then print("及格") end -- if-else语句 if score >= 90 then print("优秀") else print("继续努力") end -- if-elseif-else语句 if score >= 90 then print("优秀") elseif score >= 80 then print("良好") elseif score >= 60 then print("及格") else print("不及格") end ``` ### 嵌套条件语句 ```lua local age = 20 local hasID = true if age >= 18 then if hasID then print("可以进入") else print("需要出示证件") end else print("年龄不够") end ``` ### 条件表达式 Lua没有三元运算符,但可以使用and/or来模拟: ```lua local age = 20 local status = age >= 18 and "成年" or "未成年" print(status) -- 输出:成年 ``` ## 循环语句 ### while循环 ```lua local count = 1 -- 基本while循环 while count <= 5 do print(count) count = count + 1 end -- 条件控制的while循环 local sum = 0 local num = 1 while num <= 100 do sum = sum + num num = num + 1 end print("1到100的和:" .. sum) ``` ### repeat-until循环 ```lua local count = 1 -- 基本repeat-until循环 repeat print(count) count = count + 1 until count > 5 -- 至少执行一次的循环 local answer repeat print("请输入一个1-10之间的数字:") answer = io.read("*n") until answer >= 1 and answer <= 10 ``` ### for循环 Lua提供两种for循环:数值for和泛型for #### 1. 数值for循环 ```lua -- 基本for循环 for i = 1, 5 do print(i) end -- 指定步长的for循环 for i = 0, 10, 2 do -- 从0到10,步长为2 print(i) end -- 倒序循环 for i = 10, 1, -1 do print(i) end ``` #### 2. 泛型for循环 ```lua -- 遍历数组 local fruits = {"苹果", "香蕉", "橙子"} for i, v in ipairs(fruits) do print(i .. ": " .. v) end -- 遍历表 local person = { name = "张三", age = 25, job = "程序员" } for k, v in pairs(person) do print(k .. ": " .. tostring(v)) end ``` ## break和goto ### break语句 ```lua -- 在循环中使用break local sum = 0 for i = 1, 100 do if sum > 1000 then break -- 当和超过1000时退出循环 end sum = sum + i end print("最终和:" .. sum) -- 在while循环中使用break local count = 1 while true do if count > 5 then break end print(count) count = count + 1 end ``` ### goto语句 ```lua -- 使用goto进行流程控制 local i = 0 ::loop:: i = i + 1 print(i) if i < 5 then goto loop end -- 错误处理中使用goto local success = false if not success then goto fail end print("操作成功") goto done ::fail:: print("操作失败") ::done:: ``` ## 错误处理 ### pcall和xpcall ```lua -- 使用pcall捕获错误 local function div(a, b) if b == 0 then error("除数不能为0") end return a / b end local status, result = pcall(div, 10, 0) if status then print("结果:" .. result) else print("错误:" .. result) end -- 使用xpcall提供错误处理函数 local function errorHandler(err) print("错误发生:" .. err) print(debug.traceback()) end local status, result = xpcall(div, errorHandler, 10, 0) ``` ### assert函数 ```lua -- 使用assert进行参数检查 local function setAge(age) assert(type(age) == "number", "年龄必须是数字") assert(age >= 0 and age <= 150, "年龄必须在0到150之间") -- 设置年龄的代码 end -- 调用函数 setAge("invalid") -- 将触发错误 ``` ## 常见问题解答 ### Q1: 如何选择适当的循环结构? - `while`:当循环条件在开始时已知 - `repeat-until`:当需要至少执行一次循环体 - `for`:当知道循环的确切次数或需要遍历集合 ### Q2: break和goto的区别? - `break`:只能用于退出当前循环 - `goto`:可以跳转到任意标签位置,但要谨慎使用 ### Q3: 为什么Lua没有continue语句? - Lua的设计理念是保持语言简单 - 可以使用if语句或goto来模拟continue的功能 ```lua -- 模拟continue for i = 1, 10 do if i % 2 == 0 then goto continue end print(i) -- 只打印奇数 ::continue:: end ``` ## 下一步 现在你已经掌握了Lua的控制结构,可以继续学习[表(Table)详解](/article/lua/basic/tables)来了解Lua中最重要的数据结构。 ## 参考资源 - [Lua 5.4 参考手册](http://www.lua.org/manual/5.4/manual.html#3.3.4) - [Programming in Lua](http://www.lua.org/pil/4.html) - [Lua控制结构](http://www.lua.org/pil/4.3.html)