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("We have spaghetti dinner")elseprint("Something is missing...")end-- Output: We have spaghetti dinner
or
二元運算子 or 返回兩個參數中的一個。如果第一個參數評估為 true,則返回第一個參數。否則,返回第二個參數。
local y = x or 1print(y) -- 1 because x doesn't exist and is therefore nillocal d = falselocal e = d or 1print(e) -- 1 because d is 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("We have either spaghetti dinner OR garlic bread")elseprint("Something is missing...")end-- Output: We have either spaghetti dinner OR garlic bread
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("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。
| 運算子 | 描述 | 範例 | 關聯的元方法 |
|---|---|---|---|
| == | 等於 | 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("Hello " .. "World!") | __concat |
| # | 表格的長度 | 如果 tableVar = {1, 2, 3},則 #tableVar == 3。 | __len |