# Operators

Operators specify actions or calculations to perform on their operands, which are the values or variables which are the inputs to the operators. The most familiar operators to a beginning programmer are those that are regular mathematical operations like multiplication, divide, add, or subtract.

The equals operator performs the assignment operator. This is used to stare a value in variable.

``````local x = 12
``````

After this statement executes, the local variable x will contain the value 12.

The regular numeric operators work quite as you would expect if you are familiar with basic algebra. These operators are binary operators, which means they have two operands.

• * multiply the two numbers
• / divide the first number by the second for the quotient
• + add the two numbers
• - subtract the second number from the first

Another binary mathematical operator is the modulo operator, %. The modulo returns the remainder of integer division of the first operand by the second operand. For example:

``````print(7 % 3) -- This prints the number one.
print(8 % 4) -- This prints the number two.
``````

Some operators are unary, which means they take a single operand

• - Return the number * -1, in other words the additive inverse, the negative of the number.

The ( and ) operators are used to group expressions for purposes of changing the order of operations or precedence of the operations inside them to take place before being used in the larger context.

``````print(5*3+1)  -- This will print 16
print(5*(3+1)  -- This will print 20
``````

The string concatenation operator .., concatenates, or joins together in order two strings to make a longer string out of the two parts.

``````print("pb and" .. "jelly")  - This will print "pb and jelly".
``````

Operational operators are used to compare two values. These are used inside conditionals like if statements, and loop conditions.

• < Evaluates to true if left operand is less than right operand.
• > Evaluates to true if left operand is greater than right operand.
• <= True if left operand is less than or equal to right operand.
• >= True if left operand is greater than or equal to right operand.
• == True if the operands are equal.
• ~= True if the operands are not equal. Many other programming languages use != for not equals.

The order operators, <, >, <=, and >=, can only be used with two strings or two numbers, not a string and number together. With strings alphabetical order is used for comparing less than or greater than.

Also, if you compare values of different types the result may be surprising. For example, "1" == 1 is false.

``````x = 3
y = 4
print(x == y)
print(x ~= y)
print(x < y)
print(x > y)

y = 3
print(x <= y)
print(x >= y)

a = "red"
b = "blue"
print(a < b)
print(a <= b)
print(a >= b)
a = string.format(x)
print(a == x)  -- false!
print(a ~= x)  -- true
``````