Lua Operators
Operators are special symbols that tell the interpreter to perform specific mathematical or logical operations. Lua provides the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Other Operators
Arithmetic Operators
The following table lists the common arithmetic operators in the Lua language. Assume the value of A is 10 and the value of B is 20:
| Operator | Description | Example |
|---|---|---|
| + | Addition | A + B outputs 30 |
| - | Subtraction | A - B outputs -10 |
| * | Multiplication | A * B outputs 200 |
| / | Division | B / A outputs 2 |
| % | Modulus | B % A outputs 0 |
| ^ | Exponentiation | A^2 outputs 100 |
| - | Negative sign | -A outputs -10 |
| // | Floor division operator (>=lua5.3) | 5//2Outputs 2 |
Examples
We can understand the application of arithmetic operators more thoroughly through the following examples:
Examples
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 output of the above program is:
Line 1 - c 的值为 31 Line 2 - c 的值为 11 Line 3 - c 的值为 210 Line 4 - c 的值为 2.1 Line 5 - c 的值为 1 Line 6 - c 的值为 441 Line 7 - c 的值为 -21
In Lua,// is used for division operations, and the calculation result includes the decimal part.//// is used for floor division operations, and the calculation result does not include the decimal part:
Examples
b = 2
print("Division operation - The value of a/b is ", a / b )
print("Floor division operation - The value of a//b is ", a // b )
The output of the above program is:
除法运算 - a/b 的值为 2.5 整除运算 - a//b 的值为 2
Relational Operators
The following table lists the common relational operators in the Lua language. Assume the value of A is 10 and the value of B is 20:
| Operator | Description | Example |
|---|---|---|
| == | Equal to, checks whether two values are equal. If they are equal, returns true; otherwise, returns false. | (A == B) is false. |
| ~= | Not equal to, checks whether two values are equal. If they are not equal, returns true; otherwise, returns false. | (A ~= B) is true. |
| > | Greater than, if the left value is greater than the right value, returns true; otherwise, returns false. | (A > B) is false. |
| < | Less than, if the left value is greater than the right value, returns false; otherwise, returns true. | (A < B) is true. |
| >= | Greater than or equal to, if the left value is greater than or equal to the right value, returns true; otherwise, returns false. | (A >= B) returns false. |
| <= | Less than or equal to, if the left value is less than or equal to the right value, returns true; otherwise, returns false. | (A <= B) returns true. |
Examples
We can understand the application of relational operators more thoroughly through the following examples:
Examples
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 output of the above program is:
Line 1 - a 不等于 b Line 2 - a 不等于 b Line 3 - a 大于等于 b Line 4 - a 大于 b Line 5 - a 小于等于 b Line 6 - b 大于等于 a
Logical Operators
The following table lists the common logical operators in the Lua language. Assume the value of A is true and the value of B is false:
| Operator | Description | Example |
|---|---|---|
| and | Logical AND operator. If A is false, returns A; otherwise, returns B. | (A and B) is false. |
| or | Logical OR operator. If A is true, returns A; otherwise, returns B. | (A or B) is true. |
| not | Logical NOT operator. It is opposite to the logical operation result. If the condition is true, the logical NOT is false. | not(A and B) is true. |
Examples
We can understand the application of logical operators more thoroughly through the following examples:
Examples
b = true
if ( a and b )
then
print("a and b - The condition is true" )
end
if ( a or b )
then
print("a or b - The 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 - The condition is true" )
else
print("a and b - The condition is false" )
end
if ( not( a and b) )
then
print("not( a and b) - The condition is true" )
else
print("not( a and b) - The condition is false" )
end
The output of the above program is:
a and b - 条件为 true a or b - 条件为 true ---------分割线--------- a and b - 条件为 false not( a and b) - 条件为 true
Other Operators
The following table lists the concatenation operators and the operators for calculating the length of tables or strings in the Lua language:
| Operator | Description | Example |
|---|---|---|
| .. | Concatenates two strings | a..b, where a is "Hello " and b is "World", the output result is "Hello World". |
| # | Unary operator that returns the length of a string or table. | #"Hello" returns 5 |
Examples
We can understand the application of the concatenation operators and the operators for calculating the length of tables or strings more thoroughly through the following examples:
Examples
b = "World"
print("Concatenate strings a and b ", a..b )
print("The length of string b ",#b )
print("The length of the string Test ",#"Test" )
print("The length of the Example tutorial website URL ",#"www.example.com" )
The output of the above program is:
连接字符串 a 和 b Hello World b 字符串长度 5 字符串 Test 长度 4 Example网址长度 14
Operator Precedence
From highest to lowest:
^ not - (unary) * / % + - .. < > <= >= ~= == and or
Except for^and..all other binary operators are left-associative.
a+i < b/2+1 <--> (a+i) < ((b/2)+1) 5+x^2*8 <--> 5+((x^2)*8) a < y and y <= z <--> (a < y) and (y <= z) -x^2 <--> -(x^2) x^y^z <--> x^(y^z)
Examples
We can understand the operator precedence of the Lua language more thoroughly through the following examples:
Examples
b = 10
c = 15
d = 5
e = (a + b) * c / d;-- ( 30 * 15 ) / 5
print("(a + b) * c / d operation value is :",e )
e = ((a + b) * c) / d; -- (30 * 15 ) / 5
print("((a + b) * c) / d operation value is :",e )
e = (a + b) * (c / d);-- (30) * (15/5)
print("(a + b) * (c / d) operation value is :",e )
e = a + (b * c) / d; -- 20 + (150/5)
print("a + (b * c) / d operation value is :",e )
The output of the above program is:
(a + b) * c / d 运算值为 : 90.0 ((a + b) * c) / d 运算值为 : 90.0 (a + b) * (c / d) 运算值为 : 90.0 a + (b * c) / d 运算值为 : 50.0Other Extensions