Expressions
An expression is something that has a value. It can include numeric constants, quoted strings, variable names, unary and binary operations, and function calls.
Arithmetic operators
+
, -
, *
, /
, %
, and ^
are called arithmetic operators.
The following is an example using Binary arithmetic operators:
t = 2*(2-5.5)/13+26 print(t) -- 25.461538461538
The following is an example using the Modulo (division remainder) operator:
m = 18%4 print(m) -- 2
The following is an example using the Power of operator:
n = 7^2 print(n) --49
Relational operators
Relational operators always result in false
or true
and ask yes-or-no questions.
<
,>
, <=
,>=
, ==
, and ~=
are some of the relational operators.
The operator ==
tests for equality and the operator ~=
is the negation of equality. 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.
The following are the examples of relational operators. It will display a Boolean
result and can't be concatenated with a string:
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
Logical operators
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 operator and
returns its first argument if the value is false
or nil
; otherwise it returns its second argument. The operator or
returns its first argument if the value is different from nil
and false
; otherwise, it returns its second argument. Both and
and or
use short-cut evaluation, which means, the second operand is evaluated only when necessary.
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 operator not
always returns true
or false
:
Concatenation
The string concatenation operator in Lua is denoted by two dots ..
. It takes two strings as operands and splices them together. If any of its operands are numbers, then they are also converted to a string.
print("Hello " .. "World") -- Hello World myString = "Hello" print(myString .. " World") -- Hello World
Length operator
The length operator #
, measures the length of a string. The length of a string is simply the number of characters in it. A character is considered as 1 byte.
print(#"*") --1 print(#"\n") --1 print(#"hello") --5 myName = "Jane Doe" print(#myName) --8
Precedence
Operator precedence in Lua is as follows, from higher to lower priority:
All binary operators are left associative, except for ^
exponentiation and ..
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:
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. For example:
print (7 + 3 * 9) -- This returns the number 34
An inexperienced programmer might 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 over 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