运营商

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

运营者 是用于执行操作或条件评估的符号。

逻辑

逻辑运营器根据给定参数的 boolean 值返回值。如果一个参数不是 falsenil ,那么操作员将它评为 true 。与许多其他语言不同, Luau 将零和空字符串视为 true。下表总结了逻辑运营者在 条件中的行为方式

操作员描述
and只有两个条件都是真实的情况下才评估为 true
or如果两个条件都是真的,评估为 true 如果任何条件都是真的
not评估为条件的反义

二元运营符 and 返回两个参数之一。如果第一个参数评估为 true ,则返回第二个参数。否则,它返回第一个参数。


print(4 and 5) -- 5
print(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 循环 .例如,以下 ifthen 声明测试两个条件都是真实的:


local pasta = true
local tomatoSauce = true
if pasta == true and tomatoSauce == true then
print("We have spaghetti dinner")
else
print("Something is missing...")
end
-- Output: We have spaghetti dinner

or

二元运营符 or 返回两个参数之一。如果第一个参数评估为 true ,则返回第一个参数。否则,它返回第二个参数。


local y = x or 1
print(y) -- 1因为 x 不存在,因此为零
local d = false
local e = d or 1
print(e) -- 1因为 d 是 false
print(4 or 5) -- 4
print(nil or 12) -- 12
print(false or 12) -- 12
print(false or true) -- 真实
print(false or false) -- 错误
print(true or false) -- 真实
print(true or true) -- true

您可以使用 or控制结构 中执行复杂的逻辑测试。例如,下列 ‑ 声明测试两个条件是否真实 或者第三个条件是否真实:


local pasta = false
local tomatoSauce = true
local garlicBread = true
if (pasta == true and tomatoSauce == true) or garlicBread == true then
print("We have either spaghetti dinner OR garlic bread")
else
print("Something is missing...")
end
-- Output: We have either spaghetti dinner OR garlic bread

不是

单元操作符 not 返回参数的反向 boolean 值。如果参数是 falsenil,则返回 true。否则,它返回 false .


print(not true) -- 错误
print(not false) -- 真实
print(not nil) -- 真实
print(not "text") -- 错误
print(not 0) -- false

您可以使用 not 运营符触发条件或循环,如果变量是 falsenil


local nilVariable -- 变量声明了,但没有值,因此为零
local falseVariable = false -- 变量以假值声明值 false
if not nilVariable then
print(nilVariable) -- 输出“零”因为零不是真的
end
if not falseVariable then
print(falseVariable) -- 输出“false”因为false不是真的
end

您还可以使用 not 运营符来测试整个多条件语句的反向。在以下代码示例中,它的 if - then 条件测试不是真实的,即三不大于四或五不大于四。


local three = 3
local four = 4
local five = 5
if not (three > four or five < four) then
print("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 : truefalse

操作员描述例子相关的 metamethod
==等于3 == 5false__eq
~=不等于3 ~= 5true
>大于等于3 > 5false
<小于3 < 5true__lt
>=大于或等于3 >= 5false
<=小于或等于3 <= 5true__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 += 25
-=减法x -= 21
*=乘法x *= 26
/=分区x /= 21.5
//=楼层分割x //= 21
%=模块x %= 21
^=指数乘法x ^= 29
..=连接方式x ..= " World!" "3 World!"

杂项

杂项运营商包括 连接长度

操作员描述例子相关的 metamethod
..连接两个字符串print("Hello " .. "World!")__concat
#表的长度如果 tableVar = {1, 2, 3} , 那么 #tableVar == 3 .__len