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:
|+||Addition||Performs addition of the two operands||6+3=9|
|–||Subtraction||The second operand is subtracted from the first operand||6-3=3|
|*||Multiplication||Multiplication of the two operands||6*3=18|
|/||Division||Used to divide the numerator by the denominator||6/3=2.0|
|%||Modulus||Modulus operation is performed||6%3=0|
|^||Exponentiation||Used for exponentiation of a number||6^3=216|
|–||Negation||This operator is used for negation of a value||a = 6, -a = -6|
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:
|==||Equality||Used to check if two values are equal or not||5 == 5 returns true|
|~=||Inequality||Returns true if two values are unequal otherwise false||5 ~= 6 returns true|
|<||Less than||Returns true if the value on left side of the operator is smaller than the value on the right side otherwise false||5 < 10 returns true|
|>||Greater than||This operator returns true if value on left side of equation is greater than value on the right side otherwise false||10 > 5 returns true|
|<=||Less than or equal to||Returns true if the value on left side of the operator is less than or equal to the value on the right side otherwise false||5 <= 10 and 5<=5 both return true|
|>=||Greater than or equal to||This 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 false||10 >= 5 and 5 >= 5 both return true|
Below example is used to illustrate the relational Lua operators:
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:
|and||It returns true if both operands are true; otherwise, it returns false||true and true returns true|
|or||A true result is returned if at least one of the operands is true; otherwise, a false result is returned||true or false returns true|
|not||This operator returns the opposite of its operand||For true it returns false and vice versa.|
In the below example, logical operators in Lua have been implemented:
Lua Bitwise Operators
Bitwise Lua operators are explained in below table:
|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:
String operators in Lua are discussed below:
|..||String concatenation||Using this operator, two strings are concatenated to form another string||print(‘Mr’..’Examples’) — returns MrExamples|
|#||Length of string or table||This operator returns the length of the string or the size of a table||local str = ‘MrExamples’ print(#str) — returns 10|
Below example revolves around the string operators in Lua:
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: