Menu
Coddy logo textTech

Arithmetic Operators

Part of the Fundamentals section of Coddy's Lua journey — lesson 12 of 90.

Arithmetic operators are symbols that tell Lua to perform mathematical operations on numbers.

Lua provides four basic arithmetic operators that work just like in mathematics:

-- Addition
result = 10 + 5    -- result is 15
-- Subtraction  
result = 10 - 3    -- result is 7
-- Multiplication
result = 4 * 6     -- result is 24
-- Division
result = 15 / 3    -- result is 5
challenge icon

Challenge

Easy

Calculate the total cost of purchasing two items in a game shop. Create variables for the price of each item, then use arithmetic operators to find the total cost.

Create the following variables:

  • swordPrice with the value 25
  • shieldPrice with the value 18

Calculate the total cost by adding these two prices together and store the result in a variable called totalCost. Then print the total cost.

Cheat sheet

Lua provides four basic arithmetic operators for mathematical operations:

-- Addition
result = 10 + 5    -- result is 15

-- Subtraction  
result = 10 - 3    -- result is 7

-- Multiplication
result = 4 * 6     -- result is 24

-- Division
result = 15 / 3    -- result is 5

Try it yourself

-- Create variables for item prices
-- TODO: Write your code here

-- Calculate total cost
-- TODO: Write your code here

-- Print the total cost
print(totalCost)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals