表達式教學手冊

lua-users home
Wiki

對表達式進行評估以執行計算,計算可能會將值指派給變數,或將引數傳給函式。參考手冊第 2.5 節中對表達式有相當詳盡的說明。[1]本文將針對表達式進行說明,以提供更完整的資訊和更多範例。

我們將使用此頁面的 = 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

關係表達式

提供可回傳布林值 truefalse 的關係運算元。

範例

>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
強制轉換在此處不適用,必須明確地轉換類型。請參閱 NumbersTutorialStringsTutorial 以了解強制轉換的說明。
>"10" == 10
false
>tonumber("10") == 10
true

邏輯運算元

Lua 提供邏輯運算元 andornot。在 Lua 中,nil 和布林值 false 都代表邏輯表達式中的 false。任何不是 false 的內容(nilfalse)都是 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

關鍵字 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

二元運算元 and 並不會一定回傳布林值 truefalse 給邏輯表達式 x and y。在一些程式語言中,and 運算元會回傳一個取決於兩個輸入的布林值。反觀 Lua,如果第一個引數的值是 falsenil,它會回傳第一個引數;如果第一個引數不是 falsenil,則會回傳第二個引數。因此,只有當第一個引數為 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。
>true and false
false
>true and true
true
>print(1 and "hello", "hello" and "there")
Output : hello   there
>true and nil
nil
如同您所見,即使因為回傳的值而導致一些有趣的行為,邏輯表達式依然會被正確地評估。

or

or 二元運算元也不會一定回傳布林值(請參閱上方 and 的備註)。如果第一個引數不是 falsenil,它會回傳第一個引數;反之,就會回傳第二個引數。因此,只有當第一個引數為 true,或第二個引數為布林值時,才會回傳布林值。

>true or false
true
>true or nil
true
>print("hello" or "there", 1 or 0)
Output : hello   1
以上所有表達式都回傳第一個引數。以下所有表達式則回傳第二個引數,原因是第一個是 falsenil
>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;

此行為可以使用邏輯運算子 andor 模擬 Lua 的部分行為。C 形式

value = test ? x : y;
大致翻譯為以下的 Lua
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

但是有一個警告:這僅適用於第一個回傳值不是 nilfalse

> 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

關於測試表達式和 nil 的備註

一個要特別注意的重要點是,值 0 在 Lua 中不是錯誤的測試條件。在某些語言中,例如 C,測試

if (0)
  printf("true");
else
  printf("false");
會顯示「錯誤」。在 Lua 中
> if 0 then
>>  print("true")
>> else
>>  print("false")
>> end
true
會列印「正確」!您應該使用 falsenil 取代 0
> if false then print("true") else print("false") end
false
> if nil then print("true") else print("false") end
false

為什麼?

原因是歷史因素。在 5.0 版之前,Lua 不支援布林類型(即 truefalse)。在 5.0 版之前,nil 的值代表錯誤。現在,nilfalse 都會作為測試表達式中的錯誤條件。例如,

> 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

另一個要注意的地方是,truefalse 不是數值,例如它們在某些語言中是 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

最近變更 · 喜好設定
編輯 · 歷史記錄
最後編輯時間 2020 年 2 月 16 日,週日,上午 4:39 GMT (diff)