Operators in Lua
Operators are symbols that perform operations on variables and values. Lua provides arithmetic, relational, logical, and concatenation operators to manipulate data.
Arithmetic Operators
Basic mathematical operations:
local a = 10
local b = 3
-- Addition (+)
print(a + b) -- 13
-- Subtraction (-)
print(a - b) -- 7
-- Multiplication (*)
print(a * b) -- 30
-- Division (/)
print(a / b) -- 3.3333333333333 (always float)
-- Modulo (%) - remainder of division
print(a % b) -- 1
-- Exponentiation (^)
print(a ^ b) -- 1000 (10 * 10 * 10)
-- Unary minus (-)
print(-a) -- -10Relational (Comparison) Operators
Compare values and return true or false:
local x = 5
local y = 10
-- Equal to
print(x == y) -- false
-- Not equal to
print(x ~= y) -- true
-- Less than
print(x < y) -- true
-- Less than or equal to
print(x <= y) -- true
-- Greater than
print(x > y) -- false
-- Greater than or equal to
print(x >= y) -- falseType consideration: Relational operators always return false when comparing different types:
print(10 == "10") -- false (number vs string)Logical Operators
Lua has three logical operators: and, or, and not:
local a = true
local b = false
-- Logical AND
print(a and b) -- false
print(true and true) -- true
print(true and false) -- false
-- Logical OR
print(a or b) -- true
print(true or false) -- true
print(false or false) -- false
-- Logical NOT
print(not a) -- false
print(not b) -- trueShort-Circuit Evaluation
Logical operators use short-circuit evaluation:
-- AND: stops at first false
function test1()
print("test1 called")
return false
end
function test2()
print("test2 called")
return true
end
local result = test1() and test2() -- Only test1() is called
-- OR: stops at first true
local result = test2() or test1() -- Only test2() is calledConcatenation Operator
The .. operator concatenates (joins) strings:
local first = "Hello"
local last = "World"
local full = first .. " " .. last
print(full) -- Hello World
-- Numbers are converted to strings when concatenated
local age = 25
local message = "I am " .. age .. " years old"
print(message) -- I am 25 years old
-- Long string concatenation
local text = "Line 1\n" ..
"Line 2\n" ..
"Line 3"
print(text)Length Operator
The # operator gets the length of strings and tables:
-- String length
local text = "Hello Lua"
print(#text) -- 9
-- Table length (array-like tables)
local fruits = {"apple", "banana", "orange"}
print(#fruits) -- 3Operator Precedence
Operators have specific precedence levels (highest to lowest):
-- Precedence (highest to lowest):
-- 1. ^
-- 2. not # - (unary)
-- 3. * / %
-- 4. + -
-- 5. ..
-- 6. < > <= >= ~= ==
-- 7. and
-- 8. or
-- Example of precedence
local result = 2 + 3 * 4 -- 2 + (3 * 4) = 14
print(result)
-- Use parentheses to override precedence
local result2 = (2 + 3) * 4 -- (2 + 3) * 4 = 20
print(result2)Practical Examples
Calculator Example
local x = 10
local y = 5
-- Basic calculations
print("Sum:", x + y)
print("Difference:", x - y)
print("Product:", x * y)
print("Quotient:", x / y)
print("Remainder:", x % y)
print("Power:", x ^ 2)
-- Comparisons
if x > y then
print(x, "is greater than", y)
end
-- Logical operations
local is_positive = (x > 0) and (y > 0)
local is_even = (x % 2 == 0)
print("Both positive:", is_positive)
print("X is even:", is_even)String Manipulation
local first_name = "John"
local last_name = "Doe"
local age = 30
local full_name = first_name .. " " .. last_name
local info = "Name: " .. full_name .. ", Age: " .. age
print(full_name) -- John Doe
print(info) -- Name: John Doe, Age: 30Next Steps
Now that you understand operators, learn about control flow to make decisions in your programs.
For more operator details, see the Lua manual.
Last updated on