Lua Operators
An operator is a special symbol that tells 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
Arithmetic operators
The following table lists the commonly used arithmetic operators in the Lua language, 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 |
| % | Remainder | B % A outputs 0 |
| ^ | Exponentiation | A^2 outputs 100 |
| - | Negation | -A outputs -10 |
| // | Integer division operator (>=lua5.3) | 5//2 outputs 2 |
Example
We can understand the application of arithmetic operators more thoroughly through the following example:
Example
a
=
21
b
=
10
c
=
a
b
(
"Line 1 - the value of c is "
,
c
)
c
=
a
b
(
"Line 2 - the value of c is "
,
c
)
c
=
a
b
(
"Line 3 - the value of c is "
,
c
)
c
=
a
/
b
(
"Line 4 - the value of c is "
,
c
)
c
=
a
%
b
(
"Line 5 - the value of c is "
,
c
)
c
=
a
^
2
(
"Line 6 - the value of c is "
,
c
)
c
=
a
(
"Line 7 - the value of c is "
,
c
)
The execution result 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, and the result includes a fractional part. // is used for integer division, and the result does not include a fractional part:
Example
a
=
5
b
=
2
(
"Division - the value of a/b is "
,
a
/
b
)
(
"Integer division - the value of a//b is "
,
a
//
b
)
The execution result of the above program is:
除法运算 - a/b 的值为 2.5
整除运算 - a//b 的值为 2
Relational operators
The following table lists the commonly used relational operators in Lua, with A set to 10 and B set to 20:
| Operator | Description | Example |
|---|---|---|
| == | Equal to, checks whether two values are equal. Returns true if equal, false otherwise | (A == B) is false. |
| ~= | Not equal to, checks whether two values are equal. Returns true if not equal, false otherwise | (A ~= B) is true. |
| > | Greater than, returns true if the left value is greater than the right value, false otherwise | (A > B) is false. |
| < | Less than, returns false if the left value is greater than the right value, true otherwise | (A < B) is true. |
| >= | Greater than or equal to, returns true if the left value is greater than or equal to the right value, false otherwise | (A >= B) returns false. |
| <= | Less than or equal to, returns true if the left value is less than or equal to the right value, false otherwise | (A <= B) returns true. |
Example
We can understand the application of relational operators more thoroughly through the following example:
Example
a
=
21
b
=
10
if
(
a
==
b
)
then
(
"Line 1 - a equals b"
)
else
(
"Line 1 - a does not equal b"
)
end
if
(
a
~=
b
)
then
(
"Line 2 - a does not equal b"
)
else
(
"Line 2 - a equals b"
)
end
if
(
a
<b)then print(>"Line 3 - a is less than b"
)
else
(
"Line 3 - a is greater than or equal to b"
)
end
if
(
a
b
)
then
(
"Line 4 - a is greater than b"
)
else
(
"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
(
"Line 5 - a is less than or equal to b"
)
end
if
(
b
=
a
)
then
(
"Line 6 - b is greater than or equal to a"
)
end
The execution result 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 commonly used 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, return A; otherwise, return B. | (A and B) is false. |
| or | Logical OR operator. If A is true, return A; otherwise, return B. | (A or B) is true. |
| not | Logical NOT operator. Opposite to the logical operation result. If the condition is true, logical NOT is false. | not(A and B) is true. |
Example
We can understand the application of logical operators more thoroughly through the following example:
Example
a
=
true
b
=
true
if
(
a
and
b
)
then
(
"a and b - condition is true"
)
end
if
(
a
or
b
)
then
(
"a or b - condition is true"
)
end
(
"---------dividing line---------"
)
-- modify the values of a and b
a
=
false
b
=
true
if
(
a
and
b
)
then
(
"a and b - condition is true"
)
else
(
"a and b - condition is false"
)
end
if
(
not
(
a
and
b
)
)
then
(
"not( a and b) - condition is true"
)
else
(
"not( a and b) - condition is false"
)
end
The execution result 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 operator and the operator for computing the length of a table or string in Lua:
| Operator | Description | Example |
|---|---|---|
| .. | Concatenate 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 the application of the concatenation operator and the operator for computing the length of a table or string more thoroughly through the following example:
Example
a
=
"Hello "
b
=
"World"
(
"concatenate strings a and b "
,
a
..
b
)
(
"length of string b "
,#
b
)
(
"length of string Test "
,#
"Test"
)
(
"length of Runoob Tutorial website "
,#
"www.runoob.com"
)
The execution result of the above program is:
连接字符串 a 和 b Hello World
b 字符串长度 5
字符串 Test 长度 4
Runoob Tutorial网址长度 14
Operator Precedence
Order from high to low:
^
not - (unary)
* / %
+ -
..
< > <= >= ~= ==
and
or
All binary operators except ^ and .. 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 the precedence of Lua language operators more thoroughly through the following example:
Example
a
=
20
b
=
10
c
=
15
d
=
5
e
=
(
a
b
)
c
/
d
;
-- ( 30 * 15 ) / 5
(
"(a + b) * c / d evaluates to :"
,
e
)
e
=
(
(
a
b
)
c
)
/
d
;
-- (30 * 15 ) / 5
(
"((a + b) * c) / d evaluates to :"
,
e
)
e
=
(
a
b
)
(
c
/
d
)
;
-- (30) * (15/5)
(
"(a + b) * (c / d) evaluates to :"
,
e
)
e
=
a
(
b
c
)
/
d
;
-- 20 + (150/5)
(
"a + (b * c) / d evaluates to :"
,
e
)
The execution result 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.0