找回密码
 立即注册
    查看: 6|回复: 0

    lua 第二课整理资料

    [复制链接]

    326

    主题

    66

    回帖

    1466

    积分

    积分
    1466
    发表于 2025-3-7 21:24:28 | 显示全部楼层 |阅读模式
    条件判断
    基础条件判断(if 语句)
    高级写法(短路特性)
    判断范围
    三元运算符的模拟
    多条件判断
    循环结构
    for 循环(数值循环、倒序循环、遍历表)
    while 循环
    repeat-until 循环
    特殊用法
    判断奇偶性
    99 乘法表
    goto 语句
    计算机原理补充
    一元、二元、三元运算符
    短路运算
    1. 条件判断
    2. 1.1 基础条件判断
    3. if 语句
    4. 普通写法:
    5. lua
    6. 复制
    7. if a > 0 and b > 0 then
    8.     print("Both a and b are positive")
    9. end
    10. 高级写法(利用短路特性):
    11. lua
    12. 复制
    13. if a > 0 then
    14.     if b > 0 then
    15.         print("Both a and b are positive")
    16.     end
    17. end
    18. 判断一个值是否为真值
    19. 普通写法:
    20. lua
    21. 复制
    22. if a ~= nil and a ~= false then
    23.     print("a is true")
    24. end
    25. 高级写法:
    26. lua
    27. 复制
    28. if a then
    29.     print("a is true")
    30. end
    31. 判断范围
    32. 普通写法:
    33. lua
    34. 复制
    35. local x = 5
    36. if x > 0 and x < 10 then
    37.     print("x is between 1 and 9")
    38. end
    39. 高级写法:
    40. lua
    41. 复制
    42. local x = 5
    43. if 0 < x and x < 10 then
    44.     print("x is between 1 and 9")
    45. end
    46. 1.2 三元运算符的模拟
    47. Lua 中没有内置的三元运算符,但可以通过 条件 and 表达式1 or 表达式2 来模拟。
    48. lua
    49. 复制
    50. local a = 10
    51. local b = 20
    52. local max = (a > b) and a or b
    53. print(max)  -- 输出 20
    54. 更复杂的例子:
    55. lua
    56. 复制
    57. local score = 85
    58. local result = (score >= 90) and "优秀" or ((score >= 80) and "良好" or "及格")
    59. print(result)  -- 输出 "良好"
    60. 1.3 多条件判断
    61. 使用嵌套 if 或逻辑运算符:
    62. lua
    63. 复制
    64. local age = 18
    65. local gender = "female"
    66. local message

    67. if age >= 18 then
    68.     if gender == "male" then
    69.         message = "成年男性"
    70.     elseif gender == "female" then
    71.         message = "成年女性"
    72.     else
    73.         message = "成年,性别未知"
    74.     end
    75. else
    76.     message = "未成年"
    77. end

    78. print(message)  -- 输出 "成年女性"
    79. 2. 循环结构
    80. 2.1 for 循环
    81. 数值循环
    82. lua
    83. 复制
    84. for i = 1, 10 do
    85.     print(i)
    86. end
    87. 倒序循环
    88. lua
    89. 复制
    90. for i = 10, 1, -1 do
    91.     print(i)
    92. end
    93. 遍历表(数组)
    94. lua
    95. 复制
    96. local arr = {10, 20, 30, 40}
    97. for index, value in ipairs(arr) do
    98.     print("索引:" .. index, "值:" .. value)
    99. end
    100. 遍历表(字典)
    101. lua
    102. 复制
    103. local tbl = {a = 1, b = 2, c = 3}
    104. for key, value in pairs(tbl) do
    105.     print("键:" .. key, "值:" .. value)
    106. end
    107. 2.2 while 循环
    108. 示例:计算阶乘
    109. lua
    110. 复制
    111. local n = 5
    112. local factorial = 1

    113. while n > 0 do
    114.     factorial = factorial * n
    115.     n = n - 1
    116. end

    117. print("5 的阶乘是:" .. factorial)  -- 输出 120
    118. 2.3 repeat-until 循环
    119. 示例:至少执行一次的循环
    120. lua
    121. 复制
    122. local n = 1
    123. repeat
    124.     n = n + 1
    125. until n > 1000
    126. print("结果:" .. n)
    127. 3. 特殊用法
    128. 3.1 判断奇偶性
    129. 示例:
    130. lua
    131. 复制
    132. for i = 1, 10 do
    133.     if i % 2 == 0 then
    134.         print(i .. " 是偶数")
    135.     else
    136.         print(i .. " 是奇数")
    137.     end
    138. end
    139. 3.2 99 乘法表
    140. 示例:
    141. lua
    142. 复制
    143. for i = 1, 9 do
    144.     for j = 1, i do
    145.         io.write(j .. "x" .. i .. "=" .. i * j .. "\t")
    146.     end
    147.     print()  -- 换行
    148. end
    149. 3.3 goto 语句
    150. 示例:
    151. lua
    152. 复制
    153. print("开始")
    154. goto end_label
    155. print("这部分代码不会被执行")
    156. ::end_label::
    157. print("结束")
    158. 4. 计算机原理补充
    159. 4.1 一元、二元、三元运算符
    160. 一元运算符:需要一个操作数,例如 not、- 和 #。
    161. 示例:local a = -5 或 local b = not true
    162. 二元运算符:需要两个操作数,例如 +、*、== 和 and。
    163. 示例:local sum = a + b
    164. 三元运算符:Lua 中没有内置的三元运算符,但可以通过 条件 and 表达式1 or 表达式2 来模拟。
    165. 示例:local max = (a > b) and a or b
    166. 四元运算符:Lua 中没有四元运算符,也不常见于其他编程语言。
    167. 4.2 短路运算
    168. 逻辑与(and):如果第一个操作数为假值,则直接返回第一个操作数,不计算第二个操作数。
    169. 逻辑或(or):如果第一个操作数为真值,则直接返回第一个操作数,不计算第二个操作数。
    170. 示例:
    171. lua
    172. 复制
    173. local x = 0
    174. local result = (x == 0) and "A" or "B"
    175. print(result)  -- 输出 "A"
    176. 4.3 计算机原理补充
    177. 布尔逻辑:true 和 false 的逻辑运算。
    178. 短路特性:优化性能,避免不必要的计算。
    179. 条件表达式:如何在编程中利用布尔逻辑和短路特性简化代码。
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则