0x03 expression
arithmetic operators
Supports regular arithmetic operators: binary + - * / ^ %
and unary -
arithmetic operations are consistent with languages such as C
relational operator
Lua provides the following relational operators: <
, >
, <=
, >=
, ~=
all of which evaluate totrue
ORfalse
==
~=
For equality testing, these two operators can be applied to any two values. If the values have different types then Lua considers them unequal, otherwise Lua compares the two based on their types.
For table
userdata
and function
, Lua is a reference comparison.
a = {}; a.x = 1; a.y = 0
b = {}; b.x = 1; b.y = 0
a = c
The result of the above code isa==c
a~=b
Returns false when comparing values of different types
logical operator
Logical operators are and
or
not
, all logical operators return true
false
nil
- and
for , if the first operand is false, return the first operand, otherwise return the second operand
- or
for , if the first operand is false Returns the first operand if the operand is true, otherwise returns the second operand
a>b?a:b
In terms of and
sum or
, it is
max = (x > y) and x or y
- when x > y, return and
the following x,
- when x <= y, the or
previous expression is false, so return or
the following y
String concatenation
Join operator:..
print("Hello" .. "World") -> HelloWorld
print(0..1) -> 01
priority
Item |
---|
^ |
not # - (unary) |
* / % |
.. |
< > <= >= |
and |
or |
table construct
Arrays can be initialized or key-value pairs can be initialized
num = {"one", "two", "three"}
a = {x=10, y=20}
To start indexing with 0, you can do this:
days = {[0] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
The construct is a = {x=0, y=0}
equivalent toa = {["x"] = 0, ["y"] = 0}