An expression is something that represents a value. It can include numeric constants, quoted strings, variable names, unary and binary operations, and function calls.
+
, -
, *
, /
, %
, and ^
are called arithmetic operators.
Here is an example of binary arithmetic operators:
t = 2*(2-5.5)/13+26 print(t) -- 25.461538461538
An example of the modulo (division remainder) operator is as follows:
m = 18%4 print(m) -- 2
An example of the power of operator is as follows:
n = 7^2 print(n) -- 49
Relational operators always result in false or true and ask yes or no questions. The relational operators are <
, >
, <=
, >=
, ==
, ~=
.
The ==
operator tests for equality, and the ~=
operator tests for inequality. If the value types are different, then the result is false. Otherwise, Lua compares the values to their types. Numbers and strings are compared in the usual way. Tables and functions are compared by reference as long as two such values are considered equal, only if they are the same object. When a new object is created, the new object is different from the previously existing one.
Here are examples of relational operators. They will display Boolean results and can't be concatenated with strings:
print(0 > 1) --false print(4 > 2) --true print(1 >= 1) --true print(1 >= 1.5) --false print(0 == 0) --true print(3 == 2) --false print(2 ~= 2) -- false print(0 ~= 2) -- true
The logical operators in Lua are and
, or
, and not
. All logical operators consider both false
and nil
as false and anything else as true.
The and
operator returns its first argument if the value is false
or nil
; otherwise, it returns its second argument. The or
operator returns its first argument if the value is different from nil
and false
; otherwise, it returns its second argument. Both and
and or
use a shortcut evaluation; this means the second operand is evaluated only when necessary. Here are some examples of logical operators:
print(10 and 20) -- 20 print(nil and 1) -- nil print(false and 1) -- false print(10 or 20) -- 10 print(false or 1) -- 1
The not
operator always returns true or false:
print(not nil) -- true print(not true) -- false print(not 2) -- false
The string concatenation operator in Lua is denoted by two dots, "..
". It takes two values as operands and splices them together. If any of its operands is a number, then it is also converted to a string. Some examples of the concatenation operator are as follows:
print("Hello " .. "World") -- Hello World myString = "Hello" print(myString .. " World") -- Hello World
The #
length operator measures the length of a string or size of a table. The length of a string is simply the number of characters in it. A character is considered one byte. Examples of the length operator are as follows:
print(#"*") --1 print(#"\n") --1 print(#"hello") --5 myName = "Jane Doe" print(#myName) --8
The following list shows the operator precedence in Lua displayed from the highest to the lowest priority:
^
not
, #
, -
(unary)*
, /
+
, -
..
<
, >
, <=
, >=
, ~=
, ==
and
or
All binary operators are left associative, except for the ^
exponentiation and the..
concatenation, which are right associative. You can use parentheses to change the precedence of an expression.
In cases where two operands of the same precedence compete for operands, the operand belongs to the operator on the left-hand side:
print(5 + 4 – 2) -- This returns the number 7
The preceding expression shows both the addition and subtraction operators, which have equal precedence. The second element (the number 4
) belongs to the addition operator, so the expression is evaluated mathematically as follows:
print((5 + 4) – 2) -- This returns the number 7
Let's focus on the rules of precedence based on priority. Here is an example:
print (7 + 3 * 9) -- This returns the number 34
An inexperienced programmer may think that the value of the preceding example is 90 if it were evaluated from left to right. The correct value is 34 because multiplication has a higher precedence than addition, so it is performed first. Adding parentheses to the same expression will make it easier to read:
print (7 + (3 * 9)) -- This returns the number 34