Lua Operators
An operator is a special symbol used to 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 table below lists the common arithmetic operators in Lua, with A set to 10 and B set to 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 |
- | Unary minus | -A outputs -10 |
// | Floor division (>=lua5.3) | 5//2 outputs 2 |
Example
We can understand arithmetic operators better through the following examples:
Example
a = 21
b = 10
c = a + b
print("Line 1 - c 的值为 ", c )
c = a - b
print("Line 2 - c 的值为 ", c )
c = a * b
print("Line 3 - c 的值为 ", c )
c = a / b
print("Line 4 - c 的值为 ", c )
c = a % b
print("Line 5 - c 的值为 ", c )
c = a^2
print("Line 6 - c 的值为 ", c )
c = -a
print("Line 7 - c 的值为 ", c )
The above program execution results are:
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, which includes the decimal part, and //
is used for floor division, which excludes the decimal part:
Example
a = 5
b = 2
print("除法运算 - a/b 的值为 ", a / b )
print("整除运算 - a//b 的值为 ", a // b )
The above program execution results are:
除法运算 - a/b 的值为 2.5
整除运算 - a//b 的值为 2
Relational Operators
The table below lists the common relational operators in Lua, with A set to 10 and B set to 20:
Operator | Description | Example |
---|---|---|
== | Equality, checks if two values are equal, returns true if equal, otherwise false | (A == B) is false. |
~= | Inequality, checks if two values are not equal, returns true if not equal, otherwise false | (A ~= B) is true. |
> | Greater than, returns true if the left value is greater than the right, otherwise false | (A > B) is false. |
< | Less than, returns true if the left value is less than the right, otherwise false | (A < B) is true. |
>= | Greater than or equal to, returns true if the left value is greater than or equal to the right, otherwise false | (A >= B) is false. |
<= | Less than or equal to, returns true if the left value is less than or equal to the right, otherwise false | (A <= B) is true. |
Example
We can understand relational operators better through the following examples:
Example
a = 21
b = 10
if( a == b )
then
print("Line 1 - a 等于 b" )
else
print("Line 1 - a 不等于 b" )
end
if( a ~= b )
then
print("Line 2 - a 不等于 b" )
else
print("Line 2 - a 等于 b" )
end
if ( a < b )
then
print("Line 3 - a 小于 b" )
else
print("Line 3 - a 大于等于 b" )
end
if ( a > b )
then
print("Line 4 - a 大于 b" )
else
print("Line 5 - a 小于等于 b" )
end
-- 修改 a 和 b 的值
a = 5
b = 20
if ( a <= b )
then
print("Line 5 - a 小于等于 b" )
end
if ( b >= a )
then
print("Line 6 - b 大于等于 a" )
end
The above program execution results are:
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 table below lists the common logical operators in Lua, with A set to true and B set to 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. Returns the opposite of the logical operation. If the condition is true, logical NOT is false. | not(A and B) is true. |
Example
We can understand logical operators better through the following examples:
Example
a = true
b = true
if ( a and b )
then
print("a and b - 条件为 true" )
end
if ( a or b )
then
print("a or b - 条件为 true" )
end
print("---------分割线---------" )
-- 修改 a 和 b 的值
a = false
b = true
if ( a and b )
then
print("a and b - 条件为 true" )
else
print("a and b - 条件为 false" )
end
if ( not( a and b) )
then
print("not( a and b) - 条件为 true" )
else
print("not( a and b) - 条件为 false" )
end
The above program execution results are:
a and b - 条件为 true
a or b - 条件为 true
---------分割线---------
a and b - 条件为 false
not( a and b) - 条件为 true
Other Operators
The table below lists the concatenation operator and the length operator in Lua:
Operator | Description | Example |
---|---|---|
.. | Concatenates two strings | a..b, where a is "Hello " and b is "World", outputs "Hello World". |
# | Unary operator, returns the length of a string or table. | #"Hello" returns 5 |
Example
We can understand concatenation and length operators better through the following examples:
Example
a = "Hello "
b = "World"
print("连接字符串 a 和 b ", a..b )
print("b 字符串长度 ",#b )
print("字符串 Test 长度 ",#"Test" )
print("tutorialpro.org网址长度 ",#"www.tutorialpro.org" )
The above program execution results are:
连接字符串 a 和 b Hello World
b 字符串长度 5
字符串 Test 长度 4
tutorialpro.org网址长度 14
Operator Precedence
From highest to lowest:
^
not - (unary)
* / %
+ -
..
< > <= >= ~= ==
and
or
Except for ^
and ..
, all 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)
Example
We can understand operator precedence better through the following examples:
Example
a = 20
b = 10
c = 15
d = 5
e = (a + b) * c / d;-- ( 30 * 15 ) / 5
print("(a + b) * c / d 运算值为 :",e )
e = ((a + b) * c) / d; -- (30 * 15 ) / 5
print("((a + b) * c) / d 运算值为 :",e )
e = (a + b) * (c / d);-- (30) * (15/5)
print("(a + b) * (c / d) 运算值为 :",e )
e = a + (b * c) / d; -- 20 + (150/5)
print("a + (b * c) / d 运算值为 :",e )
The above program execution results are:
(a + b) * c / d 运算值为 : 90.0
((a + b) * c) / d 运算值为 : 90.0
(a + b) * (c / d) 运算值为 : 90.0
a + (b * c) / d 运算值为 : 50.0