表达式教程 |
|
在本页面中,我们将使用 = 表达式 简写记法。值可以轻松地赋给一个变量,例如:
>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(即不是 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;
这种行为可以通过 Lua 中的逻辑运算符 and 和 or 来部分模拟。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
这可以用于简写来填充哈希表:
> 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
这是历史原因。在 Lua 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