0Pricing
Lua Academy · Lesson

Arithmetic Operators

Add, subtract, multiply, divide.

Arithmetic Operators is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Doing Math in Lua

Lua gives you a full set of arithmetic operators to combine numbers. They work much like the math you already know.

In this lesson you will use them to add, subtract, multiply, divide, and more.

print(2 + 3)
print(10 - 4)

Addition and Subtraction

The + operator adds two numbers and - subtracts them.

You can mix integers and floats freely. If either side is a float, the result becomes a float.

local total = 5 + 2.5
print(total)

Multiplication

Use * to multiply. It follows the usual rules: two integers give an integer, and any float makes the result a float.

  • 3 * 4 is 12
  • 3 * 4.0 is 12.0
print(3 * 4)
print(6 * 1.5)

Float Division

The / operator performs float division and always returns a float.

Even when the result is whole, you will see a decimal point, like 8 / 2 giving 4.0.

print(8 / 2)
print(9 / 4)

Floor Division

The // operator divides and rounds the result down to the nearest whole number.

With two integers it gives an integer. It rounds toward negative infinity, so -7 // 2 is -4.

print(7 // 2)
print(-7 // 2)

The Modulo Operator

The % operator returns the remainder after division. It is great for checking divisibility.

For example, 10 % 3 is 1 because 3 goes into 10 three times with 1 left over.

print(10 % 3)
print(8 % 4)

Even or Odd with Modulo

A common trick: a number is even when n % 2 == 0, and odd otherwise.

This pattern shows up often in loops and conditions.

local n = 7
if n % 2 == 0 then
  print("even")
else
  print("odd")
end

Exponentiation

The ^ operator raises a number to a power. 2 ^ 3 means 2 multiplied by itself 3 times, giving 8.

Important: ^ always returns a float, so 2 ^ 3 prints 8.0.

print(2 ^ 3)
print(2 ^ 0.5)

Unary Minus

Placing - in front of a value negates it. This is called the unary minus.

It flips the sign: a positive number becomes negative and vice versa.

local x = 5
print(-x)
print(-(-3))

Operator Precedence

Lua follows standard math precedence: ^ first, then * / // %, then + -.

Use parentheses () to control the order when you need a different result.

print(2 + 3 * 4)
print((2 + 3) * 4)

Combining Operators

Real expressions often mix several operators. Lua evaluates them by precedence, then left to right.

Reading carefully (or adding parentheses) keeps your math clear and correct.

local result = 10 + 6 / 2 - 1
print(result)

Quick Check

Check your understanding of Lua arithmetic operators.

Recap

You practiced Lua's arithmetic operators: +, -, *, /, //, %, and ^, plus the unary minus.

Remember that / and ^ always return floats, // rounds down, and % gives the remainder. Precedence matters, so use parentheses for clarity.

Next, you will meet the powerful math library.

Frequently asked questions

Is the “Arithmetic Operators” lesson free?

Yes — the full text of “Arithmetic Operators” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arithmetic Operators”?

Add, subtract, multiply, divide. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arithmetic Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Integers and Floats
  2. Arithmetic Operators
  3. The math Library
  4. Number Formatting
← Back to Lua Academy