The best Lua operator Tutorial In 2024, In this tutorial you can learn Arithmetic operators,Relational Operators,Logical Operators,Other operators,Operator Precedence,
Operator is a special symbol, used to tell the interpreter to perform specific mathematical or logical operations. Lua provides the following types of operators:
The following table lists the Lua language commonly used arithmetic operators, set the value of A 10, B value of 20:
Operators | description | Examples |
---|---|---|
+ | addition | A + B output 30 |
- | Subtraction | A - B output -10 |
* | multiplication | A * B 200 output |
/ | division | B / A w output 2 |
% | Remainder | B% A output 0 |
^ | Exponentiation | A ^ 2 output 100 |
- | Minus | -A Output v -10 |
We can through the following examples to a more thorough understanding of the application of arithmetic operators:
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:
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 following table lists the Lua language commonly used relational operators, set A value of 10, B value of 20:
Operators | description | Examples |
---|---|---|
== | Equal, testing whether two values are equal, equal returns true, false otherwise | (A == B) is false. |
~ = | Is not equal to detect whether two values are equal, equal returns false, otherwise it returns true < | (A ~ = B) is true. |
> | Greater than if the value is greater than the value of the right side to the left, returns true, false otherwise | (A> B) is false. |
< | Less than, greater than the value if the value of the left to the right, and returns false, otherwise it returns true | (A <B) is true. |
> = | Greater than or equal, if the value is greater than or equal to the right of the left, it returns true, false otherwise | (A> = B) returns false. |
<= | Or less, if the value is less than or equal value to the right of the left, returns true, false otherwise | (A <= B) returns true. |
We can through the following examples to a more thorough understanding of the application of relational operators:
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:
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 following table lists the Lua language commonly used logical operators, set A value of true, B value is false:
Operators | description | Examples |
---|---|---|
and | Logical AND operator. If both are true operating condition is true. | (A and B) is false. |
or | Logical OR operator. If any operation is true on both sides of eleven condition is true. | (A or B) is true. |
not | Logical NOT operator. And logic operation contrary, if the condition is true, the logical negation is false. | not (A and B) is true. |
We can through the following examples to a more thorough understanding of the application of logical operators:
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:
a and b - 条件为 true a or b - 条件为 true ---------分割线--------- a and b - 条件为 false not( a and b) - 条件为 true
The following table lists the Lua language concatenation operator and calculation tables or the length of the string operators:
Operators | description | Examples |
---|---|---|
/en | Concatenating two strings | a/enb, which is a "Hello", b is "World", output is "Hello World". |
# | Unary operator, returns the length of string or table. | # "Hello" returns 5 |
We can through the following examples to a more thorough understanding of the connection with the operator or string length calculation table operator applications:
a = "Hello " b = "World" print("连接字符串 a 和 b ", a/enb ) print("b 字符串长度 ",#b ) print("字符串 Test 长度 ",#"Test" ) print("w3cschool本教程网址长度 ",#"www.w3cschool.cc" )
The above program execution results:
连接字符串 a 和 b Hello World b 字符串长度 5 字符串 Test 长度 4 w3cschool本教程网址长度 16
In descending order:
^ not - (unary) * / + - /en < > <= >= ~= == and or
In addition to all the outside /en ^ and binary operators 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)
We can through the following examples to a more thorough understanding of the Lua language operator precedence:
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:
(a + b) * c / d 运算值为 : 90.0 ((a + b) * c) / d 运算值为 : 90.0 (a + b) * (c / d) 运算值为 : 90.0 a + (b * c) / d 运算值为 : 50.0