控制结构和函数是任何编程语言的核心部分,Lua也不例外。
本章将详细介绍Lua中的条件语句、循环结构以及函数的定义和使用。
3.1 条件语句 (if-else)
条件语句允许程序根据不同的条件执行不同的代码块。Lua提供了灵活的if-else结构来实现条件判断。
基本if语句
最简单的if语句语法如下:
if condition then-- 代码块end
例如:
local age = 18if age >= 18 thenprint("You are an adult.")end
if-else语句
当条件不满足时,我们可能想执行另一段代码。这时可以使用if-else结构:
if condition then-- 条件为真时执行的代码else-- 条件为假时执行的代码end
例如:
local age = 16if age >= 18 thenprint("You are an adult.")elseprint("You are a minor.")end
if-elseif-else多重条件语句
对于多个条件的判断,我们可以使用if-elseif-else结构:
if condition1 then-- 代码块1elseif condition2 then-- 代码块2elseif condition3 then-- 代码块3else-- 默认代码块end
例如:
local score = 75if score >= 90 thenprint("A")elseif score >= 80 thenprint("B")elseif score >= 70 thenprint("C")elseif score >= 60 thenprint("D")elseprint("F")end
嵌套条件语句
条件语句可以嵌套使用:
local x = 10local y = 20if x > 0 thenif y > 0 thenprint("Both x and y are positive")elseprint("x is positive, but y is not")endelseprint("x is not positive")end
使用and和or进行复合条件判断
Lua允许使用逻辑运算符来组合多个条件:
local age = 25local hasLicense = trueif age >= 18 and hasLicense thenprint("You can drive")endif age < 18 or not hasLicense thenprint("You cannot drive")end
3.2 循环 (while, repeat-until, for)
循环允许我们多次执行相同的代码块。Lua提供了几种不同类型的循环结构。
while循环
while循环在条件为真时重复执行代码块:
while condition do-- 循环体end
例如:
local count = 1while count <= 5 doprint(count)count = count + 1end
repeat-until循环
repeat-until循环至少执行一次循环体,然后在条件为真时结束循环:
repeat-- 循环体until condition
例如:
local num = 1repeatprint(num)num = num + 1until num > 5
数值for循环
数值for循环用于在一个数值范围内进行迭代:
for var = start, end, step do-- 循环体end
例如:
for i = 1, 5 doprint(i)end-- 使用步长for i = 10, 1, -2 doprint(i)end
泛型for循环
泛型for循环可以遍历表或其他可迭代对象:
for key, value in pairs(table) do-- 循环体end
例如:
local fruits = {"apple", "banana", "orange"}for index, fruit in ipairs(fruits) doprint(index, fruit)end
循环控制:break和goto
break语句用于立即退出当前循环:
for i = 1, 10 doif i > 5 thenbreakendprint(i)end
Lua 5.2及以上版本支持goto语句,可以用于复杂的流程控制:
for i = 1, 10 doif i == 5 thengoto continueendprint(i)::continue::end
3.3 函数定义和调用
函数是Lua中的一等公民,可以像其他值一样被操作。
基本函数定义和调用
function greet(name)print("Hello, " .. name)endgreet("Alice") -- 输出: Hello, Alice
局部函数和全局函数
使用local关键字定义局部函数:
local function add(a, b)return a + bend
匿名函数
函数可以不需要名字,直接赋值给变量:
local multiply = function(a, b)return a * bendprint(multiply(4, 5)) -- 输出: 20
函数作为参数
function applyOperation(func, a, b)return func(a, b)endprint(applyOperation(function(x, y) return x + y end, 2, 3)) -- 输出: 5
3.4 参数和返回值
可变参数函数
使用...定义可变参数函数:
function sum(...)local result = 0for _, v in ipairs({...}) doresult = result + vendreturn resultendprint(sum(1, 2, 3, 4)) -- 输出: 10
多返回值
Lua函数可以返回多个值:
function getNameAndAge()return "Alice", 30endlocal name, age = getNameAndAge()print(name, age) -- 输出: Alice 30
3.5 闭包和递归
闭包
闭包是一个函数以及其相关的引用环境的组合:
function counter()local count = 0return function()count = count + 1return countendendlocal c = counter()print(c()) -- 输出: 1print(c()) -- 输出: 2
递归
递归是函数调用自身的过程:
function factorial(n)if n == 0 thenreturn 1elsereturn n * factorial(n - 1)endendprint(factorial(5)) -- 输出: 120
这就是Lua控制结构和函数的基本概念。
通过这些工具,你可以构建复杂的程序逻辑和算法。
