Operators in Lua: Arithmetic, Comparison, and Logical Operators
Lesson Overview
Operators are one of the most fundamental building blocks in Lua programming. They allow your program to perform calculations, compare values, and make decisions. Every meaningful Lua script—no matter how simple or complex—relies heavily on operators.
Because operators are used everywhere (conditions, loops, functions, tables, games, automation, etc.), this lesson is intentionally written as a very long, multi-part deep dive. In Part 1, we focus on Arithmetic Operators and the mathematical foundation of Lua programs.
Lesson Objectives
By the end of this complete lesson series, you will be able to:
- Understand what operators are and why they matter
- Use all arithmetic operators correctly
- Predict the result of arithmetic expressions
- Understand operator precedence
- Avoid common arithmetic mistakes in Lua
What Are Operators?
An operator is a symbol or keyword that tells Lua to perform an operation on one or more values.
Every operator works with:
- • Operands → the values being operated on
- • Operation → the action performed
- • Result → the output produced
Example:
result = 10 + 5Here:
- •
10and5are operands - •
+is the operator - •
resultstores the output
Categories of Operators in Lua
Lua operators are grouped into several categories:
This lesson focuses on the three most important categories:
- • Arithmetic
- • Comparison
- • Logical
Arithmetic Operators in Lua
Arithmetic operators are used to perform mathematical calculations.
Lua supports the following arithmetic operators:
Each operator is explained in depth below.
Addition Operator (+)
The addition operator adds two numbers.
sum = 10 + 20
print(sum)Output:
30Addition is commonly used for:
- • Scores
- • Counters
- • Totals
- • Accumulated values
Subtraction Operator (-)
The subtraction operator subtracts one number from another.
difference = 50 - 15
print(difference)Output:
35Subtraction is often used for:
- • Health reduction in games
- • Countdown timers
- • Resource consumption
Multiplication Operator (*)
The multiplication operator multiplies two numbers.
product = 6 * 7
print(product)Output:
42Multiplication is useful for:
- • Scaling values
- • Calculating areas
- • Damage multipliers
Division Operator (/)
The division operator divides one number by another.
result = 20 / 4
print(result)Output:
5.0Floor Division Operator (//)
Floor division divides numbers and rounds the result down to the nearest integer.
value = 7 // 2
print(value)Output:
3Floor division is useful when working with:
- • Grid systems
- • Index calculations
- • Discrete units
Modulus Operator (%)
The modulus operator returns the remainder of a division.
remainder = 10 % 3
print(remainder)Output:
1Modulus is commonly used for:
- • Checking even or odd numbers
- • Cycling through values
- • Timers and loops
Exponentiation Operator (^)
The exponentiation operator raises a number to a power.
power = 2 ^ 3
print(power)Output:
8Exponentiation is used for:
- • Growth formulas
- • Physics calculations
- • Advanced math
Operator Precedence (Arithmetic)
Operator precedence determines the order of execution in expressions.
Lua follows standard mathematical rules:
- Exponentiation (
^) - Multiplication, Division, Floor Division, Modulus (
*,/,//,%) - Addition and Subtraction (
+,-)
Example:
result = 10 + 2 * 5
print(result)Output:
20Multiplication happens before addition.
Using Parentheses
Parentheses override precedence.
result = (10 + 2) * 5
print(result)Output:
60Arithmetic with Variables
Operators work with variables the same way they work with numbers.
local a = 10
local b = 3
local result = a * b + 2
print(result)Common Arithmetic Mistakes
- Dividing by zero
- Forgetting operator precedence
- Using % incorrectly
- Assuming integer division
Understanding arithmetic operators prevents logic bugs.
Practice Task (Arithmetic)
- Create two number variables
- Perform all arithmetic operations on them
- Print each result
- Use parentheses to change precedence
Part 1 Summary
In this part, you learned:
- What operators are
- How arithmetic operators work
- Operator precedence
- How to control execution order
These concepts are essential for all future Lua logic.
Ready to test your knowledge?
Take the quiz to verify your understanding of operators.