運作者

*此內容是使用 AI(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 是假的
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控制結構 中執行複雜的邏輯測試。例如,下列 ifthen 聲明測試兩個條件是否真實 第三個條件是否真實:


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 運作符來測試整個多條件聲明的相反。在下面的代碼示例中,它的 ifthen 條件測試不是真實的,即三不大於四或五不大於四。


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

操作員說明範例相關的元方法
==等於3 == 5false__eq
~=不等於3 ~= 5true
>大於3 > 5false
<少於3 < 5true__lt
>=大於或等於3 >= 5false
<=小於或等於3 <= 5true__le

算術

Luau 支持通常的二進制運算符以及乘法、除法和單純否定。

操作員說明範例相關的元方法
+新增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!"

雜項

其他運作包括 連接長度

操作員說明範例相關的元方法
..連接兩個字串print("Hello " .. "World!")__concat
#表的長度如果 tableVar = {1, 2, 3} , 則 #tableVar == 3 .__len