Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The Lua operator


May 12, 2021 Lua


Table of contents


The Lua operator

An operator is a special symbol that tells the interpreter to perform a specific mathematical or logical operation. Lua offers several operator types:

  • Arithmetic operator
  • The relationship operator
  • The logical operator
  • Other operators

Arithmetic operator

The following table lists common arithmetic operators in the Lua language, setting A to 10 and B to 20:

Operator Describe Instance
+ Addition The output of A-B is 30
- Subtraction A - B output -10
* Multiplication A-B output 200
/ Division B / A w output 2
% Take the rest B % A output 0
^ Multiply the power The output of A^2 is 100
- Minus sign -A output v -10

Instance

We can better understand the application of arithmetic operators by following examples:

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 procedures are performed as follows:

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


The relationship operator

The following table lists common relationship operators in the Lua language, setting the value of A to 10 and the value of B to 20:

Operator Describe Instance
== Equal, detects whether two values are equal, and returns true equally, otherwise false is returned (A s b) is false.
~= Does not equal, detect whether the two values are equal, return false equally, or return true< The (A-B) is true.
> Greater than, if the value on the left is greater than the value on the right, return true, otherwise return false (A and B) is false.
< Less than, if the value on the left is greater than the value on the right, return false, otherwise return true (A slt; B) is true.
>= If the value on the left is greater than or equal to the value on the right, true is returned, otherwise false is returned (A >= B) is not true.
<= Less than or equal to, if the value on the left is less than or equal to the value on the right, return true, otherwise return false (A <= B) is true.

Instance

We can get a more thorough understanding of the application of relationship operators by following examples:

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 procedures are performed as follows:

Line 1 - a 不等于 b
Line 2 - a 不等于 b
Line 3 - a 大于等于 b
Line 4 - a 大于 b
Line 5 - a 小于等于  b
Line 6 - b 大于等于 a

The logical operator

The following table lists common logical operators in the Lua language, setting the value of A to true and the value of B to false:

Operator Describe Instance
and Logic and operators. If both sides of the operation are true, the condition is true. (A and B) is false.
or Logic or operators. If either side of the operation is true, the condition is true. (A or B) is true.
not Logical non-operator. Contrary to the result of the logical operation, if the condition is true, the logic is not false. Not (A and B) is true.

Instance

We can better understand the application of logical operators by following examples:

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 procedures are performed as follows:

a and b - 条件为 true
a or b - 条件为 true
---------分割线---------
a and b - 条件为 false
not( a and b) - 条件为 true


Other operators

The following table lists the connection operators in the Lua language and the operators that calculate the length of the table or string:

Operator Describe Instance
.. Connect two strings A.. b, where a is "Hello" and b is "World" and the output is "Hello World".
# A unitary operator that returns the length of a string or table. "Hello" returns 5

Instance

We can get a more thorough understanding of the application of connection operators and operators that calculate table or string length by following the following examples:

a = "Hello "
b = "World"

print("连接字符串 a 和 b ", a..b )

print("b 字符串长度 ",#b )

print("字符串 Test 长度 ",#"Test" )

print("w3cschool在线教程网址长度 ",#"www.w3cschool.cn" )

The above procedures are performed as follows:

连接字符串 a 和 b     Hello World
b 字符串长度    5
字符串 Test 长度  4
w3cschool在线教程网址长度    16

Operator priority

Order from highest to thing:

^
not    - (unary)
*      /
+      -
..
<      >      <=     >=     ~=     ==
and
or

In addition to the . . . and . . All the binary operators outside are left-connected.

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)

Instance

We can get a better understanding of the priority of the Lua language operator with the following examples:

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 procedures are performed as follows:

(a + b) * c / d 运算值为   :  90.0
((a + b) * c) / d 运算值为 :  90.0
(a + b) * (c / d) 运算值为 :  90.0
a + (b * c) / d 运算值为   :  50.0