|
条件判断
基础条件判断(if 语句)
高级写法(短路特性)
判断范围
三元运算符的模拟
多条件判断
循环结构
for 循环(数值循环、倒序循环、遍历表)
while 循环
repeat-until 循环
特殊用法
判断奇偶性
99 乘法表
goto 语句
计算机原理补充
一元、二元、三元运算符
短路运算- 条件判断
- 1.1 基础条件判断
- if 语句
- 普通写法:
- lua
- 复制
- if a > 0 and b > 0 then
- print("Both a and b are positive")
- end
- 高级写法(利用短路特性):
- lua
- 复制
- if a > 0 then
- if b > 0 then
- print("Both a and b are positive")
- end
- end
- 判断一个值是否为真值
- 普通写法:
- lua
- 复制
- if a ~= nil and a ~= false then
- print("a is true")
- end
- 高级写法:
- lua
- 复制
- if a then
- print("a is true")
- end
- 判断范围
- 普通写法:
- lua
- 复制
- local x = 5
- if x > 0 and x < 10 then
- print("x is between 1 and 9")
- end
- 高级写法:
- lua
- 复制
- local x = 5
- if 0 < x and x < 10 then
- print("x is between 1 and 9")
- end
- 1.2 三元运算符的模拟
- Lua 中没有内置的三元运算符,但可以通过 条件 and 表达式1 or 表达式2 来模拟。
- lua
- 复制
- local a = 10
- local b = 20
- local max = (a > b) and a or b
- print(max) -- 输出 20
- 更复杂的例子:
- lua
- 复制
- local score = 85
- local result = (score >= 90) and "优秀" or ((score >= 80) and "良好" or "及格")
- print(result) -- 输出 "良好"
- 1.3 多条件判断
- 使用嵌套 if 或逻辑运算符:
- lua
- 复制
- local age = 18
- local gender = "female"
- local message
- if age >= 18 then
- if gender == "male" then
- message = "成年男性"
- elseif gender == "female" then
- message = "成年女性"
- else
- message = "成年,性别未知"
- end
- else
- message = "未成年"
- end
- print(message) -- 输出 "成年女性"
- 2. 循环结构
- 2.1 for 循环
- 数值循环
- lua
- 复制
- for i = 1, 10 do
- print(i)
- end
- 倒序循环
- lua
- 复制
- for i = 10, 1, -1 do
- print(i)
- end
- 遍历表(数组)
- lua
- 复制
- local arr = {10, 20, 30, 40}
- for index, value in ipairs(arr) do
- print("索引:" .. index, "值:" .. value)
- end
- 遍历表(字典)
- lua
- 复制
- local tbl = {a = 1, b = 2, c = 3}
- for key, value in pairs(tbl) do
- print("键:" .. key, "值:" .. value)
- end
- 2.2 while 循环
- 示例:计算阶乘
- lua
- 复制
- local n = 5
- local factorial = 1
- while n > 0 do
- factorial = factorial * n
- n = n - 1
- end
- print("5 的阶乘是:" .. factorial) -- 输出 120
- 2.3 repeat-until 循环
- 示例:至少执行一次的循环
- lua
- 复制
- local n = 1
- repeat
- n = n + 1
- until n > 1000
- print("结果:" .. n)
- 3. 特殊用法
- 3.1 判断奇偶性
- 示例:
- lua
- 复制
- for i = 1, 10 do
- if i % 2 == 0 then
- print(i .. " 是偶数")
- else
- print(i .. " 是奇数")
- end
- end
- 3.2 99 乘法表
- 示例:
- lua
- 复制
- for i = 1, 9 do
- for j = 1, i do
- io.write(j .. "x" .. i .. "=" .. i * j .. "\t")
- end
- print() -- 换行
- end
- 3.3 goto 语句
- 示例:
- lua
- 复制
- print("开始")
- goto end_label
- print("这部分代码不会被执行")
- ::end_label::
- print("结束")
- 4. 计算机原理补充
- 4.1 一元、二元、三元运算符
- 一元运算符:需要一个操作数,例如 not、- 和 #。
- 示例:local a = -5 或 local b = not true
- 二元运算符:需要两个操作数,例如 +、*、== 和 and。
- 示例:local sum = a + b
- 三元运算符:Lua 中没有内置的三元运算符,但可以通过 条件 and 表达式1 or 表达式2 来模拟。
- 示例:local max = (a > b) and a or b
- 四元运算符:Lua 中没有四元运算符,也不常见于其他编程语言。
- 4.2 短路运算
- 逻辑与(and):如果第一个操作数为假值,则直接返回第一个操作数,不计算第二个操作数。
- 逻辑或(or):如果第一个操作数为真值,则直接返回第一个操作数,不计算第二个操作数。
- 示例:
- lua
- 复制
- local x = 0
- local result = (x == 0) and "A" or "B"
- print(result) -- 输出 "A"
- 4.3 计算机原理补充
- 布尔逻辑:true 和 false 的逻辑运算。
- 短路特性:优化性能,避免不必要的计算。
- 条件表达式:如何在编程中利用布尔逻辑和短路特性简化代码。
复制代码
|
|