Lua Operators: A Comprehensive Guide

In this article, we’ll focus on Lua operators and how they can be used in your Lua programs.

In Lua, operators are used to perform different kinds of operations on data values. Operators can be grouped into different categories, including:

  • Arithmetic Operators.
  • Relational Operators.
  • Logical Operators.
  • Bitwise Operators.
  • String Operators.

Lua Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numeric values.

The following table describes the arithmetic operators in Lua:

Arithmetic OperatorsFunctionsOverviewExamples
+AdditionPerforms addition of the two operands6+3=9
SubtractionThe second operand is subtracted from the first operand6-3=3
*MultiplicationMultiplication of the two operands6*3=18
/DivisionUsed to divide the numerator by the denominator6/3=2.0
%ModulusModulus operation is performed6%3=0
^ExponentiationUsed for exponentiation of a number6^3=216
NegationThis operator is used for negation of a valuea = 6, -a = -6
Note: These operators can be used with both integer and floating-point values.
The following example demonstrates arithmetic operators in Lua:


print("6+7 =",6+7) print("11-7 =",11-7) print("2*7 =",2*7) print("14/7 =",14/7) print("17%7 =",17%7) print("2^3 =",2^3) local a=2; print("-a =",-a)

Lua Relational Operators

Lua provides several relational operators that are used to compare two values and determine the relationship between them.

These operators return a boolean value (true or false) based on the result of the comparison.

Relational Lua operators are examined below:

Relational OperatorFunctionsOverviewExamples
==EqualityUsed to check if two values are equal or not5 == 5 returns true
~=InequalityReturns true if two values are unequal otherwise false5 ~= 6 returns true
<Less thanReturns true if the value on left side of the operator is smaller than the value on the right side otherwise false5 < 10 returns true
>Greater thanThis operator returns true if value on left side of equation is greater than value on the right side otherwise false10 > 5 returns true
<=Less than or equal toReturns true if the value on left side of the operator is less than or equal to the value on the right side otherwise false5 <= 10 and 5<=5 both return true
>=Greater than or equal toThis operator returns true if the value on the left side of the operator is greater than equal to the value on the right side otherwise false10 >= 5 and 5 >= 5 both return true

Below example is used to illustrate the relational Lua operators:


print("4 == 4 is",4==4) print("4 ~= 5 is",4~=5) print("9 < 8 is",9<8) print("10 > 6 is",10>6) print("12 <= 11 is",12<=11) print("15 >= 15 is",15>=15)

Lua Logical Operators

Lua Logical Operators are operators used to combine two or more expressions or conditions and produce a boolean result.

The result is true if the conditions are satisfied, and false otherwise.

The following table displays all the logical Lua operators:

Logical OperatorsOverviewExamples
andIt returns true if both operands are true; otherwise, it returns falsetrue and true returns true
orA true result is returned if at least one of the operands is true; otherwise, a false result is returnedtrue or false returns true
notThis operator returns the opposite of its operandFor true it returns false and vice versa.

In the below example, logical operators in Lua have been implemented:


print("true and true is",true and true) print("true and false is",true and false) print("true or false is",true or false) print("false or false is",false or false) print("not true is",not true)

Lua Bitwise Operators

Bitwise Lua operators are explained in below table:

Bitwise OperatorsOverview
Bitwise AND operator (&)Returns a 1 in every bit position for which the corresponding bits of both operands are 1.
Bitwise OR operator (|)Returns a 1 in each bit position where the corresponding bits of either or both operands are 1.
Bitwise exclusive OR operator ( ~ )Returns 1 in each bit position for which the corresponding bits of either but not both operands are 1.
Bitwise left shift operator (<<)Shifts the bits of the first operand to the left by the number of positions specified in the second operand.
Bitwise right shift operator (>>)Shifts the bits of the first operand to the right by the number of positions specified by the second operand.
Bitwise NOT operator (~)Flips each bit in the operand.

The following example makes use of the bitwise operators in Lua:


print("13 and 5 =",13 and 5) print("6 | 2 =",6 or 2) print("5 not 3 =",not(5 == 3)) print("5 << 2 =",5 < 2) print("7 >> 1 =",7 > 1) print("~6 =",not(6))

String Operators

String operators in Lua are discussed below:

..String concatenationUsing this operator, two strings are concatenated to form another stringprint(‘Mr’..’Examples’) — returns MrExamples
#Length of string or tableThis operator returns the length of the string or the size of a tablelocal str = ‘MrExamples’ print(#str) — returns 10

Below example revolves around the string operators in Lua:


print("Mr..Examples =","Mr".."Examples") print("#MrExamples =",#"MrExamples") print("#{3,6,9} =",#{3,6,9})

Lua Operators Precedence

Lua operators are evaluated in expressions in accordance with their precedence levels.

Following is a list of Lua operators, ranked from highest to lowest precedence:

^ (exponentiation)Right-to-left associativity
not (logical NOT)Right-to-left associativity
* (multiplication), / (division), % (modulo)Left-to-right associativity
+ (addition), – (subtraction)Left-to-right associativity
.. (string concatenation)Right-to-left associativity
< (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), ~= (not equal to), == (equal to)Left-to-right associativity
and (logical AND)Left-to-right associativity
or (logical OR)Left-to-right associativity

Higher precedence operators are evaluated before lower precedence operators. In cases where two operators have the same precedence, their evaluation order will be determined by their associativity, which can be left-to-right or right-to-left.

Some examples have been provided below for the demonstration of operator precedence in Lua:


a = 2 + 3 * 4 -- a is 14, because multiplication has higher precedence than addition print('a:',a)b = (2 + 3) * 4 -- b is 20, because parentheses can be used to override precedence print('b:',b)c = 2 ^ 3 ^ 2 -- c is 512, because exponentiation is right-associative (i.e., evaluated from right to left) print('c:',c)d = true or false and not true -- d is true, because logical NOT has higher precedence than logical AND, which has higher precedence than logical OR print('d:',d)
We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *