Expression of lua’s learning notes



The content summarized here is basically similar to that in other languages, so here is just a basic summary. No detailed explanation.

arithmetic operator

Lua supports conventional arithmetic operators such as: “+” (addition), “-” (subtraction), “*” (multiplication), “/” (Division), “^” (index), “%” (modulo), and “-” (minus sign) of unary. All of these operators are used for real numbers. For example, x ^ 0.5 will calculate the square root of X, and x ^ 3 will calculate the third power of X.

Relational operator

The relational operators provided by Lua are: “<“, “>”, “< =”, “> =”, “=”, “= =”, “~ =”; The result of all these operators is true or false.

Operator = = is used for equality test and operator ~ = is used for inequality test. These two operators can be applied to any two values. If two values have different types, Lua thinks they are not equal; In particular, nil is only equal to itself.

For table, UserData and functions, Lua is used for reference comparison. That is, they are considered equal only if they refer to the same object.

Logical operator

The logical operators provided by Lua are and, or and not. Like conditional control statements, all logical operators treat false and nil as false and everything else as true. For the operator and, if its first operand is false, it returns the first operand; Otherwise, the second operand is returned. For the operator or, if its first operand is true, it returns the first operand; Otherwise, the second operand is returned. This is different from C + + and other languages. Lua’s logical operator does not simply return true or false, but returns the value of the operand. For example, the following code:


Copy codeThe code is as follows:

print(4 and 5)               –>5
print(nil and 13)          –>nil
print(false and 13)     –>false
print(4 or 5)               –>4
print(false or 5)          –>5


And and or both use “short-circuit evaluation”, that is, they only evaluate the second operand when needed.

String connection

To concatenate two strings in Lua, you can use the operator “..” (two points). If any of its operands is a number, Lua will convert the number into a string. In Lua, the string is an immutable value, and the join operator will only create a new string without any modification to its original operand.

table constructor

Constructors are expressions used to create and initialize tables. The simplest constructor is an empty constructor {}, which is used to create an empty table. The constructor can also be used to initialize the array. The subscript of the array starts from 1. For example:

local tbDays ={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
print(tbDays[1])     -->Sunday
print(tbDays[2])     -->Monday
print(tbDays[3])     -->Tuesday
print(tbDays[4])     -->Wednesday
print(tbDays[5])     -->Thursday
print(tbDays[6])     -->Friday
print(tbDays[7])     -->Saturday

Lua also provides a special syntax for initializing table:


Copy codeThe code is as follows:

local tb1 = {x=10, y=20}
print(tb1.x)          –>10
print(tb1[“x”])          –>10


In addition, Lua also provides a more general format that allows explicit initialization of index values with an expression between square brackets, such as:


Copy codeThe code is as follows:

local tb1 = {[“+”] = “add”, [“-“] = “sub”, [“*”] = “mul”, [“/”] = “div”}


For example, the construction method of local tb1 = {x = 10, y = 20} is actually equivalent to local tb1 = {[“X”] = 10, [“Y”] = 20}. In actual programming, these two kinds of constructors can be replaced.


Pure study notes, there is nothing more to say. I hope it will be useful for everyone to learn Lua.

The above is the whole content of this article. I hope you can like it.