Operators Lua

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

A + B = 30

-

Subtracting the second operand from the first

A - B = -10

*

Multiplying two operands

A * B = 200

/

In addition to dividing the molecules with molecules

B / A = 2

%

The remainder after the modulus operator, integer division

B % A = 0

^

Operator takes the exponential power index value

A^2 = 100

-

One dollar, act as negative

-A = -10

 

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.

(A == B)Resultsfalse

~=

Check the value of two operands are equal, if the value is not equal condition becomes true.

(A ~= B)Resultstrue

> 

Check whether the value of the left operand is greater than right operand value, if so, the condition becomes true.

(A > B)Resultsfalse

< 

Check the value of the left operand is less than right operand value, if so, the condition becomes true.

(A < B)Resultstrue

>=

Check whether the value of the left operand is greater than or equal to right operand, if so, the condition becomes true.

(A >= B)Resultsfalse

<=

Check the value of the left operand is less than or equal to the value of the right operand, if so, the condition becomes true.

(A <= B)Resultstrue

 

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

and

Logical AND operator. If neither operand is zero, the condition is satisfied.

(A and B) Resultsfalse

or

Logical OR operator. If any of the two operands is non-zero, the condition becomes true.

(A or B) Resultstrue

not

Logical NOT operator. For inverting the logic state of its operand. If the condition is true, the logic will NOT operator false.

!(A and B)Resultstrue

 

4. Miscellaneous operator

Lua, supported and connected to other operators include length.

Numbering

description

Examples

...

Connecting the two strings

如果aHellobWorlda..b将返回Hello World

#

返回字符串或表长度的一元运算符。

#"Hello" 将返回 5

 

5. Lua运算符优先级

运算符优先级确定表达式中的术语分组。 这会影响表达式的计算方式。 某些运算符的优先级高于其他运算符; 例如,乘法运算符的优先级高于加法运算符 -

例如,x = 7 + 3 * 2; 这里x赋值为13,而不是20,因为运算符 * 的优先级高于+,所以它首先乘以3 * 2然后再加上7

此处,具有最高优先级的运算符显示在表的顶部,具有最低优先级的运算符显示在底部。 在表达式中,将首先评估更高优先级的运算符。

类别

操作符

关联性

一元

not # -

右到左

连接

..

右到左

乘法

* / %

左到右

加法

+ -

左到右

关系

< > <= >= == ~=

左到右

相等

== ~=

左到右

逻辑与

and

左到右

逻辑或

or

左到右

Guess you like

Origin www.cnblogs.com/gd-luojialin/p/10962725.html