Lua operators
The
operator is a special symbol used to tell the interpreter to perform a specific mathematical or logical operation. Lua provides the following operator types:
Arithmetic operators
Relational operators
Logical operators
Other operators
Arithmetic operators
The following table lists Lua Commonly used arithmetic operators in the language, set the value of A to 10 and the value of B to 20:
Operator | Description | Example |
---|---|---|
+ | Addition | A + B Output result 30 |
- | Subtraction | A - B Output result-10 |
Multiplication | A * B Output result 200 | |
Division | B / A wOutput result 2 | |
Remainder | B % A Output result 0 | |
Multiply power | A^2 Output result 100 | |
negative sign | -A output result v -10 |
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 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
Relational operator
The following table lists the commonly used relational operators in Lua language. Set the value of A to 10 and the value of B to 20:
Description | Instance | |
---|---|---|
Equal, detect whether two values are equal, return true if equal, otherwise return false | (A == B) is false. | |
is not equal, checks whether the two values are equal, returns false if equal, otherwise returns true< | (A ~= B) is true. | |
is greater than, if the value on the left is greater than the value on the right, return true, otherwise return false | (A > 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 < B) is true. | |
Greater than or equal to, if the value on the left is greater than or equal to the value on the right, return true, otherwise return false | (A > = B) returns false. | |
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) returns true. |
Operator | Description | Instance |
---|---|---|
and | Logical AND operator. The condition is true if both operations are true. | (A and B) is false. |
or | Logical OR operator. The condition is true if either operation on both sides is true. | (A or B) is true. |
not | Logical NOT operator. Contrary to the result of a logical operation, if the condition is true, logical negation is false. | not(A and B) is true. |
Examples
We can understand the application of logical operators more thoroughly through the 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 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 operators and operators for calculating table or string length in Lua language:
Operator | Description | Example |
---|---|---|
.. | Concatenate two strings | a..b, where a is "Hello", b is "World", and the output result is "Hello World". |
##Unary operator, returns the length of a string or table. | #"Hello" Return 5 |
Example
We can use the following examples to understand the connection operator and calculation table more thoroughly Or application of string length operators:
a = "Hello " b = "World" print("连接字符串 a 和 b ", a..b ) print("b 字符串长度 ",#b ) print("字符串 Test 长度 ",#"Test" ) print("w3cschoolphp中文网网址长度 ",#"www.w3cschool.cc" )
The execution result of the above program is:
连接字符串 a 和 b Hello World b 字符串长度 5 字符串 Test 长度 4 w3cschoolphp中文网网址长度 16
Operator priority
In order from high to low:
^ not - (unary) * / + - .. < > <= >= ~= == and or
All binary operators except ^ and .. 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)
Example
We can use the following example to have a more thorough understanding of the priority of Lua language operators:
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 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