An operator is a symbol for performing an operation or conditional evaluation.
逻辑
逻辑运算符根据给定参数的布尔值返回值。如果参数不是 false 或 nil,则运算符将其评估为 true。与许多其他语言不同,Luau 将零和空字符串视为 true。以下表格总结了逻辑运算符在 条件语句 中的行为。
| 运算符 | 描述 |
|---|---|
| and | 仅在两个条件都为真时评估为 true |
| or | 如果任一条件为真,则评估为 true |
| not | 评估为条件的相反值 |
and
二元运算符 and 返回两个参数中的一个。如果第一个参数评估为 true,则返回第二个参数。否则,它返回第一个参数。
print(4 and 5) -- 5print(nil and 12) -- nilprint(false and 12) -- falseprint(false and true) -- falseprint(false and false) -- falseprint(true and false) -- falseprint(true and true) -- true
您可以使用 and 在 控制结构 中测试多个条件,例如 if 语句 和 while 循环。例如,以下 if‑then 语句测试两个条件是否都为真:
local pasta = truelocal tomatoSauce = trueif pasta == true and tomatoSauce == true thenprint("我们有意大利面晚餐")elseprint("缺少一些东西...")end-- 输出: 我们有意大利面晚餐
or
二元运算符 or 返回两个参数中的一个。如果第一个参数评估为 true,则返回第一个参数。否则,它返回第二个参数。
local y = x or 1print(y) -- 1,因为 x 不存在,所以是 nillocal d = falselocal e = d or 1print(e) -- 1,因为 d 为 falseprint(4 or 5) -- 4print(nil or 12) -- 12print(false or 12) -- 12print(false or true) -- trueprint(false or false) -- falseprint(true or false) -- trueprint(true or true) -- true
您可以使用 or 在 控制结构 中执行复杂的逻辑测试。例如,以下 if‑then 语句测试两个条件是否为真 或 第三个条件是否为真:
local pasta = falselocal tomatoSauce = truelocal garlicBread = trueif (pasta == true and tomatoSauce == true) or garlicBread == true thenprint("我们有意大利面晚餐或大蒜面包")elseprint("缺少一些东西...")end-- 输出: 我们有意大利面晚餐或大蒜面包
not
一元运算符 not 返回参数的相反布尔值。如果参数为 false 或 nil,则返回 true。否则,返回 false。
print(not true) -- falseprint(not false) -- trueprint(not nil) -- trueprint(not "text") -- falseprint(not 0) -- false
您可以使用 not 运算符在变量为 false 或 nil 时触发条件或循环。
local nilVariable -- 变量被声明但没有值,因此是 nillocal falseVariable = false -- 变量被声明为 false 的值if not nilVariable thenprint(nilVariable) -- 输出 "nil" 因为 nil 不是 trueendif not falseVariable thenprint(falseVariable) -- 输出 "false" 因为 false 不是 trueend
您还可以使用 not 运算符测试整个多条件语句的相反。在以下代码示例中,if‑then 条件测试三大于四和五大于四均不为真。
local three = 3local four = 4local five = 5if not (three > four or five < four) thenprint("三小于4,且五大于4。")end-- 输出: 三小于4,且五大于4。
关系运算符
关系运算符比较两个参数并返回一个 boolean: true 或 false。
| 运算符 | 描述 | 示例 | 相关元方法 |
|---|---|---|---|
| == | 等于 | 3 == 5 → false | __eq |
| ~= | 不等于 | 3 ~= 5 → true | |
| > | 大于 | 3 > 5 → false | |
| < | 小于 | 3 < 5 → true | __lt |
| >= | 大于或等于 | 3 >= 5 → false | |
| <= | 小于或等于 | 3 <= 5 → true | __le |
算术
Luau 支持通常的二元运算符,以及指数运算、取模和一元取反。
| 运算符 | 描述 | 示例 | 相关元方法 |
|---|---|---|---|
| + | 加法 | 1 + 1 = 2 | __add |
| - | 减法 | 1 - 1 = 0 | __sub |
| * | 乘法 | 5 * 5 = 25 | __mul |
| / | 除法 | 10 / 5 = 2 | __div |
| // | 向下取整除法 | 10 // 4 = 2 | __idiv |
| ^ | 乘方运算 | 2 ^ 4 = 16 | __pow |
| % | 取模 | 13 % 7 = 6 | __mod |
| - | 一元取反 | -2 = 0 - 2 | __unm |
复合赋值
您可以使用复合赋值运算符将变量设置为操作结果,操作的第一个参数是变量的当前值。
复合赋值中的操作发生一次。例如,如果一个表达式在一个表中生成一个随机索引,Luau 将在操作和赋值中使用相同的索引。
在以下示例中,假设 local x = 3。
| 运算符 | 操作 | 示例 | 新值的 x |
|---|---|---|---|
| += | 加法 | x += 2 | 5 |
| -= | 减法 | x -= 2 | 1 |
| *= | 乘法 | x *= 2 | 6 |
| /= | 除法 | x /= 2 | 1.5 |
| //= | 向下取整除法 | x //= 2 | 1 |
| %= | 取模 | x %= 2 | 1 |
| ^= | 乘方运算 | x ^= 2 | 9 |
| ..= | 字符串连接 | x ..= " World!" | "3 World!" |
其他
其他运算符包括 字符串连接 和 长度。
| 运算符 | 描述 | 示例 | 相关元方法 |
|---|---|---|---|
| .. | 连接两个字符串 | print("你好 " .. "世界!") | __concat |
| # | 表的长度 | 如果 tableVar = {1, 2, 3},则 #tableVar == 3。 | __len |