Lua Introductory Tutorial 3. Expressions

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 userdataand 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
- andfor , if the first operand is false, return the first operand, otherwise return the second operand
- orfor , 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 andsum or, it is

max = (x > y) and x or y

- when x > y, return andthe following x,
- when x <= y, the orprevious expression is false, so return orthe 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}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325792560&siteId=291194637