OPERATORS


In programming, operators are symbols or characters that perform specific actions on values or variables. They allow you to manipulate and perform calculations on data in your program.

There are 4 types of operators:
(1) Arithmetic (addition, subtraction, etc.)
(2) Comparison (greater than, less than, etc.)
(3) Logical (manipulate conditions)
(4) Bitwise (manipulate bits in binary data)

There is one extra operator for concatenation, combining a string with other values into a longer string: (..)

a = "hello"
b = "world"
print( a .. " " .. b ) --hello world


Arithmetic Operators

These are used to manipulate numbers and perform basic mathematical operations.

+ addition
- subtraction
* multiplication
/ division
% modulo (get remainder of division)
^ exponentiation (to the power of)
--addition
3+2 --5

--subtraction
8-2 --6

--multiplication
7*3 --21

--division
40/5 --6

--modulo
10%7 --3

--exponentiation
4^3 --64


Comparative Operators

These are used to manipulate values (usually numbers) and perform basic comparisons between two values, usually in an if statement.

== equal to
~= not equal to
!= not equal to (alternative)
< less than
> greater than
<= less than or equal to
>= greater than or equal to
if a==b then print("equal") end

if a~=b then print("not equal") end

if a!=b then print("not equal") end

if a<b then print("greater than") end

if a<=b then print("less than or equal") end

if a>=b then print("greater than or equal") end

The equal and not equal comparison works with numbers, strings, booleans, and functions.


Logical Operators

These are used to manipulate or compare values (usually booleans). All logical operators consider both false and nil as false and anything else as true.

and
and Returns the first false value it encounters or the last true value if all values are true. It can be used to check multiple conditions at once. 
--booleans
print( true and true )   --true
print( true and false )  --false
print( false and true )  --false
print( false and nil )   --false
--numbers
print( 10 and 3 )        --3
print( nil and 3 )       --nil
--strings
print( "a" and "b" )     --b
print( "a" and nil )     --nil

or

or Returns the first true value it encounters or the last false value if all values are false. It can be used to choose between multiple options.
--booleans
print( false or false ) --false
print( true or false )  --true
print( false or true )  --true
print( false or nil )   --nil
--numbers
print( 10 or 3 )        --10
print( nil or 3 )       --3
--strings
print( "a" or "b" )     --a
print( nil or "b" )     --b

not

not Reverses the Boolean value of an expression. It returns "true" if the expression is false and "false" if the expression is true.
--booleans
print( not true )    --false
print( not false )   --true
--numbers
print( not -3 )      --false
print( not 0 )       --false
--strings
print( not "b" )     --false
--nil
print( not nil )     --true

A common way to use these logical operators is to combine multiple condition checks, for example:

if a and b then
	print("both true")
end

if a or b then
	print("one or both true")
end

if not a and b or c then
	print("it's complicated")
end


Bitwise Operators (advanced)

& bitwise and
| bitwise or
^^ bitwise xor
~ bitwise not
<< bitwise shift left
>> bitwise shift right
>>> bitwise logical right shift
<<> bitwise rotate left
>>< bitwise rotate right

See Bitwise Operations for more.



Precedence of Operators

Operator precedence determines the order in which operators are evaluated in an expression. It ensures that expressions are evaluated in a consistent and predictable manner. When multiple operators are used in a single expression, operator precedence helps determine which operators are evaluated first.

highest precedence

( )
^
not
* / %
+ -
..
<< >>
~
|
< > <= >= ~= ==
and
or

lowest precedence



1089

14 May 2023

Font