Back to Course HomeLesson 4 of 19
Beginner Level • Part 1

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:

LUA
result = 10 + 5

Here:

  • 10 and 5 are operands
  • + is the operator
  • result stores the output

Categories of Operators in Lua

Lua operators are grouped into several categories:

ArithmeticComparisonLogicalAssignmentUnaryConcatenation

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:

+ (Addition)- (Subtraction)* (Multiplication)/ (Division)// (Floor Division)% (Modulus)^ (Exponentiation)

Each operator is explained in depth below.

Addition Operator (+)

The addition operator adds two numbers.

LUA
sum = 10 + 20
print(sum)

Output:

Output
30

Addition is commonly used for:

  • Scores
  • Counters
  • Totals
  • Accumulated values

Subtraction Operator (-)

The subtraction operator subtracts one number from another.

LUA
difference = 50 - 15
print(difference)

Output:

Output
35

Subtraction is often used for:

  • Health reduction in games
  • Countdown timers
  • Resource consumption

Multiplication Operator (*)

The multiplication operator multiplies two numbers.

LUA
product = 6 * 7
print(product)

Output:

Output
42

Multiplication is useful for:

  • Scaling values
  • Calculating areas
  • Damage multipliers

Division Operator (/)

The division operator divides one number by another.

LUA
result = 20 / 4
print(result)

Output:

Output
5.0
Important: Lua division always returns a floating-point number.

Floor Division Operator (//)

Floor division divides numbers and rounds the result down to the nearest integer.

LUA
value = 7 // 2
print(value)

Output:

Output
3

Floor division is useful when working with:

  • Grid systems
  • Index calculations
  • Discrete units

Modulus Operator (%)

The modulus operator returns the remainder of a division.

LUA
remainder = 10 % 3
print(remainder)

Output:

Output
1

Modulus 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.

LUA
power = 2 ^ 3
print(power)

Output:

Output
8

Exponentiation 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:

  1. Exponentiation (^)
  2. Multiplication, Division, Floor Division, Modulus (*, /, //, %)
  3. Addition and Subtraction (+, -)

Example:

LUA
result = 10 + 2 * 5
print(result)

Output:

Output
20

Multiplication happens before addition.

Using Parentheses

Parentheses override precedence.

LUA
result = (10 + 2) * 5
print(result)

Output:

Output
60
Best Practice: Always use parentheses when clarity matters.

Arithmetic with Variables

Operators work with variables the same way they work with numbers.

LUA
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)

  1. Create two number variables
  2. Perform all arithmetic operations on them
  3. Print each result
  4. 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.