Data types are like elements on the periodic table. The more elements you have the more chemicals can create. Luckily there aren't as many data types as there are elements. In fact we've learned almost all of them. There are only two possible booleans:
That's right. And you can assign them to variables just like numbers, strings, nil, and functions:
myboolean = true print(myboolean)
The cool thing with numbers and strings is you can use them to create statements that can be evaluated as
Let me give an example by introducing some new operators.
Try these out in the REPL:
5 > 3
5 < 3
"5 is greater than 3" is a true statement so it returns a
Naturally, "5 is less than 3" is a false statement and returns
We can check to see if two numbers are equal in value:
number = 5 number == 5
By using a double equal (
==) we can compare the equality of two numbers.
This also works for strings:
"hello" == "hello"
"hello" == "HELLO"
For strings, often time you will see single quotes
' ' (apostrophe) used instead of regular quotes (sometimes called double quotes) wrapper around the text.
Lua doesn't care as long as the text inside both strings are identical.
We can prove that with an equality check:
'hello' == "hello"
Anyways, you can also do the inverse of an equality check and check for inequality (if two things are not equal):
5 ~= 3
"HELLO" ~= string.upper("hello")
Now let's dig in a little deeper with two more operators.
First is the
3 < 4 and 4 < 5
This reads out almost as plain English. 3 is less than 4 and 4 is less than 5. This is a logically sound statement so it evaluates to true. Just to be clear on what's actually going on here though, let's break it down. What we said is being grouped into 3 separate operations:
(3 < 4) and (4 < 5)
The two sets of parenthesis are evaluated first and internally the computer breaks these two operations down to:
(true) and (true)
True and true are both true. This sounds silly, but it is indeed logically sound. Let's try one more just to get the hang of it:
"hello" == "hello" and 6 > 10
Finally, let's try one more operator to put a bow on things.
Sometimes we don't care that both operations are correct.
We only care if one
or the other is correct.
4 == 10 or 4 ~= 10
1 > 100 or 12 == 12 or "hello" == "bananas"
As long as one of the operations is correct, the entire statement is logically true.
With the introduction of
false we've brought in a lot of new operators: "greater than" (
>), "less than" (
<), "equal" (
==), "not equal" (
~=), "and" (
and), and "or" (
- Try writing different statements with all the new operators.
- Try using two
andoperators in the same statement and see if you can make it evaluate to
- Try out these two bonus operators with some numbers: "greater than or equal to" (
>=), and "less than or equal to" (