In this chapter, we are going to dive into the operations with Lua. When start implementing your programs, you need to make good use of a comprehensive set of operators to archive your goals. For example, you may need to deal with some mathematical calculations, or compare the values of two variables or using logical gate to decide the flow of the programs. With Lua, you can easily make all these happen. Thus, no matter you’re working on a game, a simulation, or any other application, understanding these basic operations is really important.
You can use Lua Online Editor to try the examples below:
Lua Arithmetic Operators
The very basic operators should be arithmetic operators. Lua provides a set of arithmetic operators for performing basic mathematical calculations.
These include addition (+
), which sums two values, subtraction (-
), which subtracts one value from another, multiplication (*
), which multiplies two values, and division (/
), which divides one value by another. Lua also offers a modulus (%
) operator that returns the remainder of a division and exponentiation (^
), which raises one number to the power of another.
Additionally, Lua supports the unary minus (-
) operator, used to negate a number. These operators allow for flexible mathematical computations. For example, if you want to handle both simple and complex operations within Lua scripts, it would be helpful. x + y
adds two values while x ^ y
raises x
to the power of y
.
Lua Addition (+
)
The addition using operator (+
) sign adds two numbers.
Simple addition example:
local a = 10
local b = 5
local sum = a + b
print(sum) -- Output: 15
Lua Subtraction (-
)
The subtraction operator (-
) sign subtracts one number from another.
Simple subtraction example:
local a = 50
local b = 7
local difference = a - b
print(difference) -- Output: 43
Lua Multiplication (*
)
The multiplication operator (*
) is used to multiplies two numbers. Instead of using “x”, we normally use “*” for multiplication in most of the programming languages.
Simple multiplication example
local a = 7
local b = 8
local product = a * b
print(product) -- Output: 56
Lua Division (/
)
The division operator (/
) divides one number by another. Again, instead of using “÷” in math, we use / for division in most of the coding languages.
Simple division example
local a = 144
local b = 12
local quotient = a / b
print(quotient) -- Output: 12
Modulus (%
)
Sometime you may need to find the remainder. Then you can use the modulus operator (%
) returns the remainder of the division of one number by another.
Finding the remainder by modulus operator
local a = 100
local b = 30
local remainder = a % b
print(remainder) -- Output: 10
Exponentiation (^
)
Finally, the exponentiation operator (^
) raises one number to the power of another.
Lua exponential
local a = 2
local b = 3
local power = a ^ b
print(power) -- Output: 8
Lua Scripts demonstrate all the arithmetic operators
Here’s like a summary to show case all the knowledge we just learned. Feel free to test it out with your own Lua editor, or use the online lua compiler provided at the beginning.
-- Define variables
local a = 10
local b = 5
-- Perform arithmetic operations
local sum = a + b
local difference = a - b
local product = a * b
local quotient = a / b
local remainder = a % b
local power = a ^ b
-- Print results
print("Sum: " .. sum) -- Output: Sum: 15
print("Difference: " .. difference) -- Output: Difference: 5
print("Product: " .. product) -- Output: Product: 50
print("Quotient: " .. quotient) -- Output: Quotient: 2
print("Remainder: " .. remainder) -- Output: Remainder: 0
print("Power: " .. power) -- Output: Power: 100000