[Lua from bronze to the king] Chapter 5: Lua operator

Posted by Aliz on Mon, 07 Mar 2022 09:22:20 +0100

Catalogue of series articles

preface

🌲 1, Lua operator

  • An operator is a special symbol that tells the interpreter to perform a specific mathematical or logical operation. Lua provides the following operator types:
  • Arithmetic operator
  • Relational operator
  • Logical operator
  • Other Operators

🌲 1. Arithmetic operator

1. Grammar

The following table lists the common arithmetic operators in Lua language. Set the value of A as 10 and the value of B as 20:

2. Examples

We can more thoroughly understand the application of arithmetic operators through the following examples:

--Arithmetic operator + - * /(division) %(Surplus) ^(power) -(minus sign)  //(divisible)

a = 21
b = 10
c = a + b
print("Line 1 - c The value of is ", c )
c = a - b
print("Line 2 - c The value of is ", c )
c = a * b
print("Line 3 - c The value of is ", c )
c = a / b
print("Line 4 - c The value of is ", c )
c = a % b
print("Line 5 - c The value of is ", c )
c = a^2
print("Line 6 - c The value of is ", c )
c = -a
print("Line 7 - c The value of is ", c )

The results of the above procedures are:

Line 1 - c The value of is     31
Line 2 - c The value of is     eleven
Line 3 - c The value of is     two hundred and ten
Line 4 - c The value of is     2.1
Line 5 - c The value of is     1
Line 6 - c The value of is     four hundred and forty-one
Line 7 - c The value of is     -21


In lua, / is used as a division operation, and the calculation result contains decimal parts, / / is used as an integer division operation, and the calculation result does not contain decimal parts:

a = 5
b = 2

print("Division operation - a/b The value of is ", a / b )
print("Division operation - a//The value of B is "a // b" 

The results of the above procedures are as follows:

Division operation - a/b The value of is 2.5
 Division operation - a//The value of b is 2

🌳 2. Relational operators

1. Grammar

The following table lists the common relational operators in Lua language. Set the value of A as 10 and the value of B as 20:

2. Examples

We can more thoroughly understand the application of relational operators through the following examples:

--[[--]]
--Relational operator
a = 21
b = 10

if( a == b )
then
   print("Line 1 - a be equal to b" )
else
   print("Line 1 - a Not equal to b" )
end

if( a ~= b )
then
   print("Line 2 - a Not equal to b" )
else
   print("Line 2 - a be equal to b" )
end

if ( a < b )
then
   print("Line 3 - a less than b" )
else
   print("Line 3 - a Greater than or equal to b" )
end

if ( a > b )
then
   print("Line 4 - a greater than b" )
else
   print("Line 5 - a Less than or equal to b" )
end

-- modify a and b Value of
a = 5
b = 20
if ( a <= b )
then
   print("Line 5 - a Less than or equal to  b" )
end

if ( b >= a )
then
   print("Line 6 - b Greater than or equal to a" )
end

The results of the above procedures are as follows:

Line 1 - a Not equal to b
Line 2 - a Not equal to b
Line 3 - a Greater than or equal to b
Line 4 - a greater than b
Line 5 - a Less than or equal to  b
Line 6 - b Greater than or equal to a

🌴 3. Logical operators

1. Grammar

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

2. Examples

We can more thoroughly understand the application of logical operators through the following examples:

--Logical operator

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("---------Split line---------" )

-- modify a and b Value of
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 results of the above procedures are as follows:

a and b - Condition is true
a or b - Condition is true
---------Split line---------
a and b - Condition is false
not( a and b) - Condition is true

🌵 4. Other operators

1. Grammar

The following table lists the join operators and operators for calculating the length of a table or string in Lua language:

2. Examples

We can more thoroughly understand the application of join operators and operators for calculating table or string length through the following examples:

--Other Operators 

a="hello"
b="world"

print("Connection string a and b ",a..b)

print("b String length ",#b )

print("character string test Length of ",#'test' )

print("character string www.baidu.com Length of ",#'www.baidu.com' )

print("Length of string wujige",#"Brother Wuji")

The results of the above procedures are as follows:

Connection string a and b 	helloworld

b String length 	5

character string test Length of 	4

character string www.baidu.com Length of 	13

Length of string wujige	9


be careful:




🌲 5. Operator priority

1. Grammar

Order from high to low:

All binary operators except ^ and... Are left connected.

--Operator priority except ^ and .. All 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)

2. Examples

We can more thoroughly understand the priority of Lua language operators through the following examples:

a = 20
b = 10
c = 15
d = 5

e = (a + b) * c / d;-- ( 30 * 15 ) / 5
print("(a + b) * c / d The operation value is  :",e )

e = ((a + b) * c) / d; -- (30 * 15 ) / 5
print("((a + b) * c) / d The operation value is :",e )

e = (a + b) * (c / d);-- (30) * (15/5)
print("(a + b) * (c / d) The operation value is :",e )

e = a + (b * c) / d;  -- 20 + (150/5)
print("a + (b * c) / d The operation value is   :",e )

The results of the above procedures are as follows:

(a + b) * c / d The operation value is  :    90.0
((a + b) * c) / d The operation value is :    90.0
(a + b) * (c / d) The operation value is :    90.0
a + (b * c) / d The operation value is   :    50.0


be careful:

rx = 1
ry = 2
rz = 3


print(rx and ry, ry and rx)    --Output 2    1
print(rx or ry, ry or rx)      --Output 1    2
print(rx and ry or rz)         --Output 2
print(rx or ry and rz)         --Output 1


💬🌲🌳🌴🌵 summary

The above is what we want to talk about today. This paper introduces the Lua operator and the skilled use of operators. Learning these operators will become very convenient for future operations, so we must master them. In addition, if there are any problems above, please understand my brother's advice, but it doesn't matter. It's mainly because I can insist. I hope some students who study together can help me correct them. But if you can be gentle, please tell me that love and peace are the eternal theme and love you all.

Topics: Back-end lua