Expressions Tutorial |
|
We'll use the =
expression shorthand notation for this page. The values can easily be assigned to a variable, e.g.,
> x = 7
> print(x)
7
> = 7
7
Lua has the usual binary arithmetic operators.
> = 2+3, 5-12, 2*7, 7/8 5 -7 14 0.875 > = 5*(2-8.3)/77.7+99.1 98.694594594595
> = -(-10), -(10) 10 -10
> = 15%7, -4%3, 5.5%1 1 2 0.5
> = 7^2, 107^0, 2^8 49 1 256
Relational operators are supplied which return the boolean values true
or false
.
==
equal to
~=
not equal to
<
less than
>
greater than
<=
less than or equal to
>=
greater than or equal to
Examples:
> = 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 provides the logical operators and
, or
and not
. In Lua both nil
and the boolean value false
represent false in a logical expression. Anything that is not false (either nil
or false
) is true
. There are more notes on the implications of this at the end of this page.
> = 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. no, false. nil
The keyword not
inverts a logical expression value:
> = 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
The binary operator and
does not necessarily return a boolean value true
or false
to the logical expression x and y. In some languages the and
operator returns a boolean dependent on the two inputs. Rather in Lua, it returns the first argument if its value is false
or nil
, and the second argument if the first argument is not false
or nil
. So, a boolean is only returned if the first argument is false
or the second argument is a boolean.
> = 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 > = 1 and "hello", "hello" and "there" hello there > = true and nil nil
The or
binary operator also does not necessarily return a boolean value (see notes for and
above). If the first argument is not false
or nil
it is returned, otherwise the second argument is returned. So, a boolean is only returned if the first argument is true
or the second argument is a boolean.
> = true or false true > = true or nil true > = "hello" or "there", 1 or 0 hello 1
false
or nil
.
> = false or true true > = nil or true true > = nil or "hello" hello
This can be a very useful property. For example, setting default values in a function:
> 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
Ternary operators [2] are a useful feature in C. e.g.
int value = x>3 ? 1 : 0;
This behaviour can be partially emulated in Lua using the logical operators and
and or
. The C form:
value = test ? x : y;
value = test and x or y
E.g.
> print( 3>1 and 1 or 0 ) 1 > print( 3<1 and 1 or 0 ) 0 > print( 3<1 and "True" or "False" ) False > print( 3>1 and true or "false" ) true
This can be used for short hand to fill a 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 1 13 2 14 3 1
However, there is a caveat: This only works when the first return value is not nil
or false
.
> print( 3>1 and 1 or "False" ) -- works 1 > print( 3>1 and false or "oops" ) -- failed, should return false oops > print( 3>1 and nil or "oops" ) -- failed, should return nil oops
An important point to note is that the value 0
is not a false test condition in Lua. In some languages, for example C, a test of:
if (0) printf("true"); else printf("false");
> if 0 then >> print("true") >> else >> print("false") >> end true
false
, or nil
in place of 0:
> if false then print("true") else print("false") end false > if nil then print("true") else print("false") end false
The reason for this is historical. Lua did not support boolean types (i.e. true
and false
) before version 5.0. Previous to version 5.0 a value of nil
represented false. Now, both nil
and false
will act as a false condition in a test expression. E.g.,
> 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
Another point to note is that true
and false
are not numerical values, e.g., 1 and 0 as they are in some languages.
> = true, false true false > = 1 + true stdin:1: attempt to perform arithmetic on a boolean value stack traceback: stdin:1: in main chunk [C]: ?
Also, nil
is coerced into a boolean value when used with a logical operator:
> = not nil true > = not 1 false > = not 0 false