English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
An operator is a special symbol used to tell the interpreter to perform a specific mathematical or logical operation. Lua provides the following types of operators:
Arithmetic operators
Relational operators
Logical operators
Other operators
The following table lists the commonly used arithmetic operators in the Lua language, setting the value of A to10, the value of B is 20:
Operator | Description | Example |
---|---|---|
+ | Addition | A + B output result 30 |
- | Subtraction | A - B output result -10 |
* | Multiplication | A * B output result 200 |
/ | Division | B / A w output result 2 |
% | Modulo | B % A output result 0 |
^ | Power | A^2 Output result 100 |
- | Negative sign | -A output result -10 |
We can understand the application of arithmetic operators more thoroughly through the following examples:
a = 21 b = 10 c = a + b print("Line 1 - The value of c is " , c ) c = a - b print("Line 2 - The value of c is " , c ) c = a * b print("Line 3 - The value of c is " , c ) c = a / b print("Line 4 - The value of c is " , c ) c = a % b print("Line 5 - The value of c is " , c ) c = a^2 print("Line 6 - The value of c is " , c ) c = -a print("Line 7 - The value of c is " , c )
The result of the above program is:
Line 1 - The value of c is 31 Line 2 - The value of c is 11 Line 3 - The value of c is 210 Line 4 - The value of c is 2.1 Line 5 - The value of c is 1 Line 6 - The value of c is 441 Line 7 - The value of c is -21
The following table lists the commonly used relational operators in the Lua language, setting the value of A to10, the value of B is 20:
Operator | Description | Example |
---|---|---|
== | Equal, checks if two values are equal, returns true if they are equal, otherwise returns false | (A == B) is false. |
~= | Not equal, checks if two values are equal, returns true if they are not equal, otherwise returns false | (A ~= B) is true. |
> | Greater than, returns true if the value on the left is greater than the value on the right, otherwise returns false | (A > B) is false. |
< | Less than, returns false if the value on the left is greater than the value on the right, otherwise returns true | (A < B) is true. |
>= | Greater than or equal to, returns true if the value on the left is greater than or equal to the value on the right, otherwise returns false | (A >= B) returns false. |
<= | Less than or equal to, returns true if the value on the left is less than or equal to the value on the right, otherwise returns false | (A <= B) returns true. |
We can understand the application of relational operators more thoroughly through the following examples:
a = 21 b = 10 if( a == b ) then print("Line 1 - a equals b" ) else print("Line 1 - a does not equal b" ) end if( a ~= b ) then print("Line 2 - a does not equal b" ) else print("Line 2 - a equals b" ) end if(a < b) then print("Line 3 - a is less than b) else print("Line 3 - a is greater than or equal to b) end if(a > b) then print("Line 4 - a is greater than b) else print("Line 5 - a is less than or equal to b) end -- Modify the values of a and b a = 5 b = 20 if(a <= b) then print("Line 5 - a is less than or equal to b) end if(b >= a) then print("Line 6 - b is greater than or equal to a) end
The result of the above program is:
Line 1 - a is not equal to b Line 2 - a is not equal to b Line 3 - a is greater than or equal to b Line 4 - a is greater than b Line 5 - a is less than or equal to b Line 6 - b is greater than or equal to a
The following table lists the commonly used logical operators in the Lua language, with A set to true and B set to false:
Operator | Description | Example |
---|---|---|
and | Logical AND operator. If A is false, it returns A, otherwise it returns B. | (A and B) is false. |
or | Logical OR operator. If A is true, it returns A, otherwise it returns B. | (A or B) is true. |
not | Logical NOT operator. It is the opposite of the logical operation result, if the condition is true, logical NOT is false. | not(A and B) is true. |
We can understand the application of logical operators more thoroughly through the following examples:
a = true b = true if(a and b) then print("a and b - Condition is true) end if(a or b) then print("a or b - Condition is true) end print("---------Separator---------" -- Modify the values of a and b a = false b = true if(a and b) then print("a and b - Condition is true) else print("a and b - Condition is false) end if(not(a and b)) then print("not(a and b) - Condition is true) else print("not(a and b) - Condition is false) end
The result of the above program is:
a and b - Condition is true a or b - Condition is true ---------Separator--------- a and b - Condition is false not(a and b) - Condition is true
The following table lists the concatenation operators and operators for calculating table or string length in the Lua language:
Operator | Description | Example |
---|---|---|
.. | Concatenate two strings | a..b, where a is "Hello " and b is "World", the output is "Hello World". |
# | Unary operator, returns the length of the string or table. | #"Hello" returns 5 |
We can understand the application of the concatenation operator and the operator for calculating table or string length more thoroughly through the following examples:
a = "Hello " b = "World" print("Concatenate strings a and b", a..b ) print("b string length", #b ) print("The length of the string 'Test' ", #"Test") print("The length of the Basic Tutorial website URL ", #"www.w")3codebox.com"
The result of the above program is:
Concatenate strings a and b 'Hello World' The length of string b 5 The length of the string 'Test' 4 The length of the Basic Tutorial website URL 14
In order from high to low:
^ not - (unary) * / % + - .. < > <= >= ~= == and or
All binary operators except ^ and .. are left-associative.
a+i < b/2+1 <--> x^(a < y) and (y <= z)+i) < ((b/2)+1) 5+x^2*8 <--> 5+((x^2)*8) x^(y^z) <--> x^(y^z) < -x^2 <--> -(x^2) x^y^z <--> x^(y^z)
We can use the following examples to better understand the precedence of Lua language operators:
a = 20 b = 10 c = 15 d = 5 e = (a + b) * c / d;-- ( 30 * 15 ) / 5 print("(a + b) * c / d) The value of: e = ((a + b) * c) / d; -- (30 * 15 ) / 5 print("((a + b) * c) / d) The value of: e = (a + b) * (c / d);-- (30) * (15/5) print("(a + b) * (c / d) The value of: e = a + (b * c) / d; -- 20 + (150/5) print("a + (b * c) / d) The value of:
The result of the above program is:
(a + b) * c / d) The value of: 90.0 ((a + b) * c) / d) The value of: 90.0 (a + b) * (c / d) The value of: 90.0 a + (b * c) / The value of d is: 50.0