Lua - Operators
Hello, budding programmers! Today, we're going to embark on an exciting journey into the world of Lua operators. Don't worry if you've never coded before – I'll be your friendly guide, and we'll explore this topic step by step. By the end of this lesson, you'll be manipulating data like a pro!
Arithmetic Operators
Let's start with something familiar: math! Lua provides us with arithmetic operators that work just like the ones you've used in math class.
Basic Arithmetic Operators
Here's a table of the basic arithmetic operators in Lua:
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 10 - 4 = 6 |
* | Multiplication | 3 * 4 = 12 |
/ | Division | 20 / 5 = 4 |
% | Modulus (remainder) | 17 % 5 = 2 |
^ | Exponentiation | 2 ^ 3 = 8 |
Let's see these in action:
print(5 + 3) -- Output: 8
print(10 - 4) -- Output: 6
print(3 * 4) -- Output: 12
print(20 / 5) -- Output: 4
print(17 % 5) -- Output: 2
print(2 ^ 3) -- Output: 8
Each line performs a different arithmetic operation and prints the result. The --
symbol indicates a comment, which is ignored by Lua but helps us understand the code.
The Modulus Operator
The modulus operator (%
) might be new to some of you. It gives us the remainder after division. Think of it as asking, "If I divide these numbers, what's left over?" For example, when we divide 17 by 5, we get 3 with a remainder of 2. That's why 17 % 5
equals 2.
Relational Operators
Now, let's move on to relational operators. These are used to compare values and always result in either true
or false
.
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 is true |
~= | Not equal to | 5 ~= 4 is true |
> | Greater than | 7 > 3 is true |
< | Less than | 2 < 8 is true |
>= | Greater than or equal to | 5 >= 5 is true |
<= | Less than or equal to | 4 <= 4 is true |
Let's try these out:
print(5 == 5) -- Output: true
print(5 ~= 4) -- Output: true
print(7 > 3) -- Output: true
print(2 < 8) -- Output: true
print(5 >= 5) -- Output: true
print(4 <= 4) -- Output: true
Remember, in Lua, we use ~=
for "not equal to", which is different from some other programming languages that use !=
.
Logical Operators
Logical operators are used to combine conditional statements. They're like the "and", "or", and "not" in everyday language.
Operator | Description |
---|---|
and | True if both operands are true |
or | True if at least one operand is true |
not | Reverses the logical state |
Here's how we use them:
print(true and false) -- Output: false
print(true or false) -- Output: true
print(not true) -- Output: false
-- We can use these with variables too
local x = 5
local y = 10
print(x < 10 and y > 5) -- Output: true
print(x > 10 or y < 5) -- Output: false
In the last example, x < 10 and y > 5
is true because both conditions are true: x is less than 10, and y is greater than 5.
Misc Operators
Lua has a couple of other operators that don't fit neatly into the previous categories:
Operator | Description | Example |
---|---|---|
.. | Concatenation (joining strings) | "Hello " .. "World" = "Hello World" |
# | Length operator | #"Hello" = 5 |
Let's see these in action:
print("Hello " .. "World") -- Output: Hello World
print(#"Hello") -- Output: 5
The concatenation operator (..
) is super useful when you want to combine strings. The length operator (#
) tells you how many characters are in a string.
Operators Precedence in Lua
Just like in math, Lua has an order of operations. Here's the precedence from highest to lowest:
- ^
- not # - (unary)
-
- /
-
-
- ..
- < > <= >= ~= ==
- and
- or
This means that in an expression like 2 + 3 * 4
, Lua will first do the multiplication (3 * 4 = 12) and then the addition (2 + 12 = 14).
If you want to change the order, you can use parentheses. For example:
print(2 + 3 * 4) -- Output: 14
print((2 + 3) * 4) -- Output: 20
In the second line, the parentheses tell Lua to do the addition first, then multiply the result by 4.
And there you have it! You've just taken your first steps into the world of Lua operators. Remember, practice makes perfect. Try combining these operators in different ways and see what happens. Don't be afraid to make mistakes – that's how we learn!
In my years of teaching, I've found that the best programmers are the ones who aren't afraid to experiment. So go ahead, play around with these operators, and see what you can create. Who knows? You might just be the next great Lua programmer!
Credits: Image by storyset