控制结构和函数是任何编程语言的核心部分,Lua也不例外。

本章将详细介绍Lua中的条件语句、循环结构以及函数的定义和使用。

3.1 条件语句 (if-else)

条件语句允许程序根据不同的条件执行不同的代码块。Lua提供了灵活的if-else结构来实现条件判断。

基本if语句

最简单的if语句语法如下:

  1. if condition then
  2. -- 代码块
  3. end

例如:

  1. local age = 18
  2. if age >= 18 then
  3. print("You are an adult.")
  4. end

if-else语句

当条件不满足时,我们可能想执行另一段代码。这时可以使用if-else结构:

  1. if condition then
  2. -- 条件为真时执行的代码
  3. else
  4. -- 条件为假时执行的代码
  5. end

例如:

  1. local age = 16
  2. if age >= 18 then
  3. print("You are an adult.")
  4. else
  5. print("You are a minor.")
  6. end

if-elseif-else多重条件语句

对于多个条件的判断,我们可以使用if-elseif-else结构:

  1. if condition1 then
  2. -- 代码块1
  3. elseif condition2 then
  4. -- 代码块2
  5. elseif condition3 then
  6. -- 代码块3
  7. else
  8. -- 默认代码块
  9. end

例如:

  1. local score = 75
  2. if score >= 90 then
  3. print("A")
  4. elseif score >= 80 then
  5. print("B")
  6. elseif score >= 70 then
  7. print("C")
  8. elseif score >= 60 then
  9. print("D")
  10. else
  11. print("F")
  12. end

嵌套条件语句

条件语句可以嵌套使用:

  1. local x = 10
  2. local y = 20
  3. if x > 0 then
  4. if y > 0 then
  5. print("Both x and y are positive")
  6. else
  7. print("x is positive, but y is not")
  8. end
  9. else
  10. print("x is not positive")
  11. end

使用and和or进行复合条件判断

Lua允许使用逻辑运算符来组合多个条件:

  1. local age = 25
  2. local hasLicense = true
  3. if age >= 18 and hasLicense then
  4. print("You can drive")
  5. end
  6. if age < 18 or not hasLicense then
  7. print("You cannot drive")
  8. end

3.2 循环 (while, repeat-until, for)

循环允许我们多次执行相同的代码块。Lua提供了几种不同类型的循环结构。

while循环

while循环在条件为真时重复执行代码块:

  1. while condition do
  2. -- 循环体
  3. end

例如:

  1. local count = 1
  2. while count <= 5 do
  3. print(count)
  4. count = count + 1
  5. end

repeat-until循环

repeat-until循环至少执行一次循环体,然后在条件为真时结束循环:

  1. repeat
  2. -- 循环体
  3. until condition

例如:

  1. local num = 1
  2. repeat
  3. print(num)
  4. num = num + 1
  5. until num > 5

数值for循环

数值for循环用于在一个数值范围内进行迭代:

  1. for var = start, end, step do
  2. -- 循环体
  3. end

例如:

  1. for i = 1, 5 do
  2. print(i)
  3. end
  4. -- 使用步长
  5. for i = 10, 1, -2 do
  6. print(i)
  7. end

泛型for循环

泛型for循环可以遍历表或其他可迭代对象:

  1. for key, value in pairs(table) do
  2. -- 循环体
  3. end

例如:

  1. local fruits = {"apple", "banana", "orange"}
  2. for index, fruit in ipairs(fruits) do
  3. print(index, fruit)
  4. end

循环控制:break和goto

break语句用于立即退出当前循环:

  1. for i = 1, 10 do
  2. if i > 5 then
  3. break
  4. end
  5. print(i)
  6. end

Lua 5.2及以上版本支持goto语句,可以用于复杂的流程控制:

  1. for i = 1, 10 do
  2. if i == 5 then
  3. goto continue
  4. end
  5. print(i)
  6. ::continue::
  7. end

3.3 函数定义和调用

函数是Lua中的一等公民,可以像其他值一样被操作。

基本函数定义和调用

  1. function greet(name)
  2. print("Hello, " .. name)
  3. end
  4. greet("Alice") -- 输出: Hello, Alice

局部函数和全局函数

使用local关键字定义局部函数:

  1. local function add(a, b)
  2. return a + b
  3. end

匿名函数

函数可以不需要名字,直接赋值给变量:

  1. local multiply = function(a, b)
  2. return a * b
  3. end
  4. print(multiply(4, 5)) -- 输出: 20

函数作为参数

  1. function applyOperation(func, a, b)
  2. return func(a, b)
  3. end
  4. print(applyOperation(function(x, y) return x + y end, 2, 3)) -- 输出: 5

3.4 参数和返回值

可变参数函数

使用...定义可变参数函数:

  1. function sum(...)
  2. local result = 0
  3. for _, v in ipairs({...}) do
  4. result = result + v
  5. end
  6. return result
  7. end
  8. print(sum(1, 2, 3, 4)) -- 输出: 10

多返回值

Lua函数可以返回多个值:

  1. function getNameAndAge()
  2. return "Alice", 30
  3. end
  4. local name, age = getNameAndAge()
  5. print(name, age) -- 输出: Alice 30

3.5 闭包和递归

闭包

闭包是一个函数以及其相关的引用环境的组合:

  1. function counter()
  2. local count = 0
  3. return function()
  4. count = count + 1
  5. return count
  6. end
  7. end
  8. local c = counter()
  9. print(c()) -- 输出: 1
  10. print(c()) -- 输出: 2

递归

递归是函数调用自身的过程:

  1. function factorial(n)
  2. if n == 0 then
  3. return 1
  4. else
  5. return n * factorial(n - 1)
  6. end
  7. end
  8. print(factorial(5)) -- 输出: 120

这就是Lua控制结构和函数的基本概念。

通过这些工具,你可以构建复杂的程序逻辑和算法。