Operator performs symbol tells the interpreter to specific mathematical or logical operations. Lua language rich built-in operators, and provide the following types of operators -
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Other operators
This tutorial will explain each arithmetic relationships, and other miscellaneous logic operators.
1. Arithmetic Operators
The following table shows all the arithmetic operators Lua language support. Assume that the variable A=10
, variable B=20
, and then -
Operators |
description |
Examples |
|
Adding two operands |
|
|
Subtracting the second operand from the first |
|
|
Multiplying two operands |
|
|
In addition to dividing the molecules with molecules |
|
|
The remainder after the modulus operator, integer division |
|
|
Operator takes the exponential power index value |
|
|
One dollar, act as negative |
|
2. Relational Operators
The following table shows the Lua language supported by all relational operators. Assume that the variable A=10
, variable B=20
, and then -
Operators |
description |
Examples |
|
Check the value of two operands are equal, if they are equal, the condition becomes true. |
|
|
Check the value of two operands are equal, if the value is not equal condition becomes |
|
|
Check whether the value of the left operand is greater than right operand value, if so, the condition becomes |
|
|
Check the value of the left operand is less than right operand value, if so, the condition becomes |
|
|
Check whether the value of the left operand is greater than or equal to right operand, if so, the condition becomes |
|
|
Check the value of the left operand is less than or equal to the value of the right operand, if so, the condition becomes |
|
3. Logical Operators
The following table shows all of the logical operators Lua language support. Assume that the variable A=true
, variable B=false
, then -
Operators |
description |
Examples |
|
Logical AND operator. If neither operand is zero, the condition is satisfied. |
|
|
Logical OR operator. If any of the two operands is non-zero, the condition becomes true. |
|
|
Logical NOT operator. For inverting the logic state of its operand. If the condition is true, the logic will NOT operator |
|
4. Miscellaneous operator
Lua, supported and connected to other operators include length.
Numbering |
description |
Examples |
|
Connecting the two strings |
如果 |
|
返回字符串或表长度的一元运算符。 |
|
5. Lua运算符优先级
运算符优先级确定表达式中的术语分组。 这会影响表达式的计算方式。 某些运算符的优先级高于其他运算符; 例如,乘法运算符的优先级高于加法运算符 -
例如,x = 7 + 3 * 2
; 这里x
赋值为13
,而不是20
,因为运算符 *
的优先级高于+
,所以它首先乘以3 * 2
然后再加上7
。
此处,具有最高优先级的运算符显示在表的顶部,具有最低优先级的运算符显示在底部。 在表达式中,将首先评估更高优先级的运算符。
类别 |
操作符 |
关联性 |
一元 |
|
右到左 |
连接 |
|
右到左 |
乘法 |
|
左到右 |
加法 |
|
左到右 |
关系 |
|
左到右 |
相等 |
|
左到右 |
逻辑与 |
|
左到右 |
逻辑或 |
|
左到右 |