运营者 是用于执行操作或条件评估的符号。
逻辑
逻辑运营器根据给定参数的 boolean 值返回值。如果一个参数不是 false 或 nil ,那么操作员将它评为 true 。与许多其他语言不同, Luau 将零和空字符串视为 true。下表总结了逻辑运营者在 条件中的行为方式。
操作员 | 描述 |
---|---|
and | 只有两个条件都是真实的情况下才评估为 true |
or | 如果两个条件都是真的,评估为 true 如果任何条件都是真的 |
not | 评估为条件的反义 |
和
二元运营符 and 返回两个参数之一。如果第一个参数评估为 true ,则返回第二个参数。否则,它返回第一个参数。
print(4 and 5) -- 5print(nil and 12) -- 无print(false and 12) -- 错误print(false and true) -- 错误print(false and false) -- 错误print(true and false) -- 错误print(true and true) -- true
您可以使用 and 来在 控制结构 中测试多个条件,例如 if 语句 和 while 循环 .例如,以下 if ‑ then 声明测试两个条件都是真实的:
local pasta = truelocal tomatoSauce = trueif pasta == true and tomatoSauce == true thenprint("We have spaghetti dinner")elseprint("Something is missing...")end-- Output: We have spaghetti dinner
or
二元运营符 or 返回两个参数之一。如果第一个参数评估为 true ,则返回第一个参数。否则,它返回第二个参数。
local y = x or 1print(y) -- 1因为 x 不存在,因此为零local 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) -- 真实print(false or false) -- 错误print(true or false) -- 真实print(true or true) -- true
您可以使用 or 在 控制结构 中执行复杂的逻辑测试。例如,下列 ‑ 声明测试两个条件是否真实 或者第三个条件是否真实:
local pasta = falselocal tomatoSauce = truelocal garlicBread = trueif (pasta == true and tomatoSauce == true) or garlicBread == true thenprint("We have either spaghetti dinner OR garlic bread")elseprint("Something is missing...")end-- Output: We have either spaghetti dinner OR garlic bread
不是
单元操作符 not 返回参数的反向 boolean 值。如果参数是 false 或 nil,则返回 true。否则,它返回 false .
print(not true) -- 错误print(not false) -- 真实print(not nil) -- 真实print(not "text") -- 错误print(not 0) -- false
您可以使用 not 运营符触发条件或循环,如果变量是 false 或 nil 。
local nilVariable -- 变量声明了,但没有值,因此为零local falseVariable = false -- 变量以假值声明值 falseif not nilVariable thenprint(nilVariable) -- 输出“零”因为零不是真的endif not falseVariable thenprint(falseVariable) -- 输出“false”因为false不是真的end
您还可以使用 not 运营符来测试整个多条件语句的反向。在以下代码示例中,它的 if - then 条件测试不是真实的,即三不大于四或五不大于四。
local three = 3local four = 4local five = 5if not (three > four or five < four) thenprint("Three is less than 4 and five is greater than 4.")end-- Output: Three is less than 4 and five is greater than 4.
关联的
关联运营者比较两个参数并返回一个 boolean : true 或 false 。
操作员 | 描述 | 例子 | 相关的 metamethod |
---|---|---|---|
== | 等于 | 3 == 5 → false | __eq |
~= | 不等于 | 3 ~= 5 → true | |
> | 大于等于 | 3 > 5 → false | |
< | 小于 | 3 < 5 → true | __lt |
>= | 大于或等于 | 3 >= 5 → false | |
<= | 小于或等于 | 3 <= 5 → true | __le |
算法
Luau支持通常的二元运算符以及指数、乘数和单元负数。
操作员 | 描述 | 例子 | 相关的 metamethod |
---|---|---|---|
+ | 添加 | 1 + 1 = 2 | __add |
- | 减法 | 1 - 1 = 0 | __sub |
* | 乘法 | 5 * 5 = 25 | __mul |
/ | 分区 | 10 / 5 = 2 | __div |
// | 楼层分割 | 10 // 4 = 2 -10 // 4 = -3 | __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!" |
杂项
杂项运营商包括 连接 和 长度 。
操作员 | 描述 | 例子 | 相关的 metamethod |
---|---|---|---|
.. | 连接两个字符串 | print("Hello " .. "World!") | __concat |
# | 表的长度 | 如果 tableVar = {1, 2, 3} , 那么 #tableVar == 3 . | __len |