Mathematical Operators
Lua can even be used as a calculator! Lua uses the following mathematic operators:
Addition [+] print(2 + 2) --> 4
Subtraction [-] print(10 - 7) --> 3
Multiplication [*] print(5 * 5) --> 25
Division [/] print(12 / 3) --> 4
Exponents [^] print(3^2) --> 9
Modulus [%] print(13 % 7) --> 6
You might not be familiar with some of these symbols or terms, but don't worry.
There are many other mathematical functions, though. (math.pi, math.floor, etc.) I recommend checking them out at this wiki page. You can even perform arithmetic on variables: x = 3; print(x * 9) --> 27
Order of Operations
What does 7 - 3 * 2 equal? If you answered 8, Lua doesn't agree with you. Lua uses order of operations, or PEMDAS. (You may know it as BOMDAS)
PEMDAS is an acronym:
Parentheses
Exponents
Multiplication/
Division
Addition/
Subtraction
This means that Lua performs arithmetic in parentheses first. It then does multiplication/division in the order it comes. This is followed by addition/subtraction in the order it comes. So, 7 - 3 * 2 = 1. We do the multiplication (3 * 2 = 6) and then we subtract 6 from 7. 7 - 6 = 1. Understand?
You can practice with this: (12 - 5) * 5 + 3^2 / 9
Parentheses - 12 - 5 = 7
Exponents - 3^2 = 9
Multiplication - 7 * 5 = 35
Division - 9 / 9 = 1
Addition - 35 + 1 = 36
So the answer is 36.
Addition [+] print(2 + 2) --> 4
Subtraction [-] print(10 - 7) --> 3
Multiplication [*] print(5 * 5) --> 25
Division [/] print(12 / 3) --> 4
Exponents [^] print(3^2) --> 9
Modulus [%] print(13 % 7) --> 6
You might not be familiar with some of these symbols or terms, but don't worry.
There are many other mathematical functions, though. (math.pi, math.floor, etc.) I recommend checking them out at this wiki page. You can even perform arithmetic on variables: x = 3; print(x * 9) --> 27
Order of Operations
What does 7 - 3 * 2 equal? If you answered 8, Lua doesn't agree with you. Lua uses order of operations, or PEMDAS. (You may know it as BOMDAS)
PEMDAS is an acronym:
Parentheses
Exponents
Multiplication/
Division
Addition/
Subtraction
This means that Lua performs arithmetic in parentheses first. It then does multiplication/division in the order it comes. This is followed by addition/subtraction in the order it comes. So, 7 - 3 * 2 = 1. We do the multiplication (3 * 2 = 6) and then we subtract 6 from 7. 7 - 6 = 1. Understand?
You can practice with this: (12 - 5) * 5 + 3^2 / 9
Parentheses - 12 - 5 = 7
Exponents - 3^2 = 9
Multiplication - 7 * 5 = 35
Division - 9 / 9 = 1
Addition - 35 + 1 = 36
So the answer is 36.
Relational Operators
Relational Operators are used when comparing 2 values. They return a boolean (true or false) and are often used with if statements. Lua's relational operators are:
[==] Tests equality: 5 == 5 --> true, 9 == 2 --> false
[~=] Tests inequality: 12 ~= 7 --> true, 53 ~= 53 --> false
[>] Greater than: 7 > 5 --> true, 5 > 7 --> false
[<] Less than: 9 < 90 --> true, 300 < math.pi --> false
[>=] Greater than or equal to: 5 >= 2 --> true, 7 >= 7 --> true
[<=] Less than or equal to: 2 <= 6 --> true, 9 <= 3 --> false, 1 <= 1 --> true
You don't always need to use numbers, though. When testing equality, you can use most, if not all, values. You can even use variables:
x = 5
y = 7
z = "hi"
print(x > 2) --> true
print(x == y) --> false
print("hi" == z) --> true
TIP: You can use multiple sets of parentheses in the same line:
print(5*((2+3)+x^2))
Now you can head over to the next tutorial.
Further reading:ROBLOX Wiki, Programming in Lua
[==] Tests equality: 5 == 5 --> true, 9 == 2 --> false
[~=] Tests inequality: 12 ~= 7 --> true, 53 ~= 53 --> false
[>] Greater than: 7 > 5 --> true, 5 > 7 --> false
[<] Less than: 9 < 90 --> true, 300 < math.pi --> false
[>=] Greater than or equal to: 5 >= 2 --> true, 7 >= 7 --> true
[<=] Less than or equal to: 2 <= 6 --> true, 9 <= 3 --> false, 1 <= 1 --> true
You don't always need to use numbers, though. When testing equality, you can use most, if not all, values. You can even use variables:
x = 5
y = 7
z = "hi"
print(x > 2) --> true
print(x == y) --> false
print("hi" == z) --> true
TIP: You can use multiple sets of parentheses in the same line:
print(5*((2+3)+x^2))
Now you can head over to the next tutorial.
Further reading:ROBLOX Wiki, Programming in Lua