언어/lua

5. 연산자

조규현15 2015. 12. 1. 09:03
반응형

Arithmetic Operators

Following table shows all the arithmetic operators supported by Lua language. Assume variable A holds 10 and variable B holds 20 then:

Show Examples

OperatorDescriptionExample
+Adds two operandsA + B will give 30
-Subtracts second operand from the firstA - B will give -10
*Multiply both operandsA * B will give 200
/Divide numerator by de-numeratorB / A will give 2
%Modulus Operator and remainder of after an integer divisionB % A will give 0
^Exponent Operator takes the exponentsA^2 will give 100
-Unary - operator acts as negation-A will give -10

Relational Operators

Following table shows all the relational operators supported by Lua language. Assume variable A holds 10 and variable B holds 20 then:

Show Examples

OperatorDescriptionExample
==Checks if the value of two operands is equal or not, if yes then condition becomes true.(A == B) is not true.
~=Checks if the value of two operands is equal or not, if values are not equal then condition becomes true.(A ~= B) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(A <= B) is true.

Logical Operators

Following table shows all the logical operators supported by Lua language. Assume variable A holds true and variable B holds false then:

Show Examples

OperatorDescriptionExample
andCalled Logical AND operator. If both the operands are non zero then condition becomes true.(A and B) is false.
orCalled Logical OR Operator. If any of the two operands is non zero then condition becomes true.(A or B) is true.
notCalled Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(A and B) is true.

Misc Operators

Miscellaneous operators supported by Lua Language include concatenation and length.

Show Examples

OperatorDescriptionExample
..Concatenates two strings.a..b where a is "Hello " and b is "World", will return "Hello World".
#An unary operator that return the length of the a string or a table.#"Hello" will return 5

Operators Precedence in Lua

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedence than + so it first get multiplied with 3*2 and then adds into 7.

Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Show Examples

Category Operator Associativity 
Unary not # - Right to left 
Concatenation .. Right to left 
Multiplicative  * / % Left to right 
Additive  + - Left to right 
Relational  < > <= >= == ~= Left to right 
Equality  == ~= Left to right 
Logical AND and Left to right 
Logical OR or Left to right 


반응형

'언어 > lua' 카테고리의 다른 글

8. 함수  (0) 2015.12.01
6. 중간 정리  (0) 2015.12.01
4. 자료형  (0) 2015.12.01
3. 함수  (0) 2015.12.01
2. 개발환경 세팅  (0) 2015.12.01