First Steps with Lua - OperatorsDeutsch


You are here:LuaOperators



Operators

Basic Arithmetics

Negative Numbers get a leading - (Minus)

Binary operators:

Operator Description Example
+ Addition a = b + c
- Subtraction a = b - c
(negatives leading sign)
* Multiplication a = b * c
/ Division a= b / c
^ Potentialisation a = b ^ c


Rail-Diagramm binop
Test-Programm calculation_1.lua
print( '2 + 3 =', 2 + 3 )
print( '2 - 3 =', 2 - 3 )
print( '2 * 3 =', 2 * 3 )
print( '2 / 3 =', 2 / 3 )
print( '2 ^ 3 =', 2 ^ 3 )
The Result:
2 + 3 =	5
2 - 3 =	-1
2 * 3 =	6
2 / 3 =	0.66666666666667
2 ^ 3 =	8

Comparison

= =
Equality
∼=
Imparity
<
Lesser
<=
Less equal
>
Bigger
>=
Bigger equal

Test-Programm compare_1.lua
print( '2 == 3 ', 2 == 3 )
print( '2 ~= 3 ', 2 ~= 3 )
print( '2 >  3 ', 2 >  3 )
print( '2 <  3 ', 2 <  3 )
print( '2 >= 3 ', 2 >= 3 )
print( '2 <= 3 ', 2 <= 3 )
The Result:
2 == 3 	false
2 ~= 3 	true
2 >  3 	false
2 <  3 	true
2 >= 3 	false
2 <= 3 	true
Test-Programm compare_2.lua
print( "'A' == 'B' ", 'A' == 'B' )
print( "'A' ~= 'B' ", 'A' ~= 'B' )
print( "'A' >  'B' ", 'A' >  'B' )
print( "'A' <  'B' ", 'A' <  'B' )
print( "'A' >= 'B' ", 'A' >= 'B' )
print( "'A' <= 'B' ", 'A' <= 'B' )
The Result:
'A' == 'B' 	false
'A' ~= 'B' 	true
'A' >  'B' 	false
'A' <  'B' 	true
'A' >= 'B' 	false
'A' <= 'B' 	true
Test-Programm compare_3.lua
print( "'a' == 'A' ", 'a' == 'A' )
print( "'a' ~= 'A' ", 'a' ~= 'A' )
print( "'a' >  'A' ", 'a' >  'A' )
print( "'a' <  'A' ", 'a' <  'A' )
print( "'a' >= 'A' ", 'a' >= 'A' )
print( "'a' <= 'A' ", 'a' <= 'A' )
The Result:
'a' == 'A' 	false
'a' ~= 'A' 	true
'a' >  'A' 	true
'a' <  'A' 	false
'a' >= 'A' 	true
'a' <= 'A' 	false

Logical Operators


Rail-Diagramm unop

not
Logical No
and
Logical and
or
logical or

Test-Programm logic_1.lua
print( "true and false =", true and false )
print( "true or false =", true or false )
The Result:
true and false =	false
true or false =	true

Braces in logical Expressions

Test-Programm logic_2.lua
print( " true or  false  and false  =",  true or  false  and false  )
print( " true or (false  and false) =",  true or (false  and false) )
print( "(true or  false) and false  =", (true or  false) and false  )
The Result:
 true or  false  and false  =	true
 true or (false  and false) =	true
(true or  false) and false  =	false

Braces in logical Expressions with Negation (not)

Test-Programm logic_3.lua
print( "not true and false	=", not true and false )
print( "not (true and false)	=", not (true and false) )
print( "true and not false	=", true and not false )
print( "not true or false	=", not true or false )
print( "not (true or false)	=", not (true or false) )
print( "true or not false	=", true or not false )
The Result:
not true and false	=	false
not (true and false)	=	true
true and not false	=	true
not true or false	=	false
not (true or false)	=	false
true or not false	=	true