表達式教學手冊 |
|
我們將使用此頁面的 =
expression 速記符號。可輕易將值指派給變數,例如:
>x = 7
>print(x)
Output : 7
Lua 具有一般的二元算術運算元。
>print(2+3, 5-12, 2*7, 7/8) Output : 5 -7 14 0.875 >print(5*(2-8.3)/77.7+99.1) Output : 98.694594594595
>print(-(-10), -(10)) Output : 10 -10 Note: --10 will make it become a comment, so make sure you type it right like this: -(-10). Don't forget the parantheses
>print(15%7, -4%3, 5.5%1)
Output: 1 2 0.5
>print(7^2, 107^0, 2^8)
Output : 49 1 256
提供可回傳布林值 true
或 false
的關係運算元。
==
等於~=
不等於<
小於>
大於<=
小於或等於>=
大於或等於範例
>1 == 1, 1 == 0 true false >1 ~= 1, 1 ~= 0 false true >3 < 7, 7 < 7, 8 < 7 true false false >3 > 7, 7 > 7, 8 > 7 false false true >3 <= 7, 7 <= 7, 8 <= 7 true true false >3 >= 7, 7 >= 7, 8 >= 7 false true true
>"abc" < "def" true >"abc" > "def" false >"abc" == "abc" true >"abc" == "a".."bc" true
>{} == "table" false >{} == {} -- two different tables are created here false > t = {} > t2 = t >t == t2 -- we're referencing the same table here true
>"10" == 10 false >tonumber("10") == 10 true
Lua 提供邏輯運算元 and
、or
和 not
。在 Lua 中,nil
和布林值 false
都代表邏輯表達式中的 false。任何不是 false 的內容(nil
或 false
)都是 true
。本頁面末尾有更多說明這項含意的備註。
>false==nil -- although they represent the same thing they are not equivalent false >true==false, true~=false false true >1==0 false >does_this_exist -- test to see if variable "does_this_exist" exists. If no, then it will return nil. nil
關鍵字 not
可反轉邏輯表達式值
>true, false, not true, not false true false false true >not nil -- nil represents false true >not not true -- true is not not true! true >not "foo" -- anything not false or nil is true false
New example(2020) >local bruh = false >if not bruh then >>print("hai") >end Output : hai --If not bruh is the same as If bruh == false >local bruh = true >if bruh then >>print("hai") >end Output: hai --If bruh is the same as If bruh == true
二元運算元 and
並不會一定回傳布林值 true
或 false
給邏輯表達式 x and y。在一些程式語言中,and
運算元會回傳一個取決於兩個輸入的布林值。反觀 Lua,如果第一個引數的值是 false
或 nil
,它會回傳第一個引數;如果第一個引數不是 false
或 nil
,則會回傳第二個引數。因此,只有當第一個引數為 false
,或第二個引數為布林值時,才會回傳布林值。
>false and true -- false is returned because it is the first argument false >nil and true -- as above nil >nil and false nil >nil and "hello", false and "hello" nil false
>true and false false >true and true true >print(1 and "hello", "hello" and "there") Output : hello there >true and nil nil
or
二元運算元也不會一定回傳布林值(請參閱上方 and
的備註)。如果第一個引數不是 false
或 nil
,它會回傳第一個引數;反之,就會回傳第二個引數。因此,只有當第一個引數為 true
,或第二個引數為布林值時,才會回傳布林值。
>true or false true >true or nil true >print("hello" or "there", 1 or 0) Output : hello 1
false
或 nil
。>false or true true >nil or true true >print(nil or "hello") Output : hello
這是一個很有用的屬性,例如設定函式中的預設值
> function foo(x) >> local value = x or "default" -- if argument x is false or nil, value becomes "default" >> print(value, x) >> end > > foo() -- no arguments, so x is nil default nil > foo(1) 1 1 > foo(true) true true > foo("hello") hello hello
三元運算子 [2] 是 C 中有用的功能。例如
int value = x>3 ? 1 : 0;
此行為可以使用邏輯運算子 and
和 or
模擬 Lua 的部分行為。C 形式
value = test ? x : y;
value = test and x or y
例如
> print( 3>1 and 1 or 0 ) Output : 1 > print( 3<1 and 1 or 0 ) Output : 0 > print( 3<1 and "True" or "False" ) False > print( 3>1 and true or "false" ) true
這可當作填入 hash 的簡寫
> t = {} > t[1] = 12; > t[2] = 13; > for i=1, 3 do >> t[i] = (t[i] or 0) + 1 >end > for k, v in pairs(t) do >> print(k, v); > end Output : 1 13 2 14 3 1
但是有一個警告:這僅適用於第一個回傳值不是 nil
或 false
。
> print( 3>1 and 1 or "False" ) -- works Output : 1 > print( 3>1 and false or "oops" ) -- failed, should return false Output : oops > print( 3>1 and nil or "oops" ) -- failed, should return nil Output : oops
一個要特別注意的重要點是,值 0
在 Lua 中不是錯誤的測試條件。在某些語言中,例如 C,測試
if (0) printf("true"); else printf("false");
> if 0 then >> print("true") >> else >> print("false") >> end true
false
或 nil
取代 0> if false then print("true") else print("false") end false > if nil then print("true") else print("false") end false
原因是歷史因素。在 5.0 版之前,Lua 不支援布林類型(即 true
和 false
)。在 5.0 版之前,nil
的值代表錯誤。現在,nil
和 false
都會作為測試表達式中的錯誤條件。例如,
> if nil then print("true") else print("false") end false > if 1 then print("true") else print("false") end true > if 0 then print("true") else print("false") end true > if 1==2 then print("true") else print("false") end false
另一個要注意的地方是,true
和 false
不是數值,例如它們在某些語言中是 1 和 0。
> = true, false true false > = 1 + true stdin:1: attempt to perform arithmetic on a boolean value stack traceback: stdin:1: in main chunk [C]: ?
此外,當與邏輯運算子一起使用時,nil
會轉換為布林值
>not nil true >not 1 false >not 0 false