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.

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

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.

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