Overview
So far, you have done computations data, writing code that includes everything you want to do in order, executed as soon as you run the program. In this lesson you will learn how to write conditional code, that only executes when a condition you define is true.
We will be using Booleans for this, as well as other operations that evaluate to true
or false
.
Equality
One way to get a result of true or false is to test if two values are equal. You do this with ==
.
Example:
local a = 5
local b = 7
a == b
Code language: Lua (lua)
Assignment vs. Comparison
You may have noticed that we already used =
to create variables. Although they use the same punctuation, assigning a value to a variable and comparing a value are very different operations.
=
is a command: Make this equal to this.
==
is a question: Is this equal to this?
Example:
local aTruth = true
local aFalsehood = false
print(aTruth == aFalsehood)
aTruth = aFalsehood
print(aTruth == aFalsehood)
Code language: Lua (lua)
Strings and Numbers
You can also use ==
to compare strings and numbers.
Strings will need to match precisely, including having the exact same capitalization. Numbers will never match strings.
Examples:
local a = "a String"
local b = "a string"
print(a == b)
local stringNumber = "7"
local actualNumber = 7
print(stringNumber == actualNumber)
Code language: Lua (lua)
Not Equal
You can use the ~=
operator to evaluate whether two values are NOT equivalent.
local aTruth = true
local aFalsehood = false
print(aTruth ~= aFalsehood)
Code language: Lua (lua)
Inequalities
Inequalities allow you to check if a numerical value is bigger or smaller than another. This can be useful when you are checking for a minimum number of things.
Greater Than and Less Than
The greater than operator >
checks if the value on the left is greater, and the less than operator <
checks if the left value is less than the right one.
Examples:
print(10 > 4)
print(1 < -3)
Code language: Lua (lua)
Or Equal to
You can also check if a value is greater than or equal to another value or less than or equal to one. This is great for scenarios where you need “at least” or “at most” of something.
Examples:
local playersConnected = 3
print(playersConnected >= 3)
print(playersConnected <= 16)
Code language: Lua (lua)
Logical Operators
Often you will need to check more than one logical condition before running some code. Logical operators will allow you to do this.
And
Use the keyword and
to check if two logical statements are BOTH true.
This can be great to check if a number is between two other values.
Examples:
local aTruth = true
local aFalsehood = false
print(aTruth and aFalsehood)
local a = 10
print (a > 2 and a < 20)
Code language: Lua (lua)
Or
Use the keyword or
when at least one of the parts must be true. If both side of an or
are true, it will also be true.
Examples:
local iLikeCoffee = true
local iLikeTea = false
print(iLikeCoffee or iLikeTea)
Code language: Lua (lua)
Not
You can use the keyword not
to get the opposite of the truth value.
Examples:
local iLikeCoffee = true
local iDontLikeCoffee = not iLikeCoffee
print(iDontLikeCoffee)
Code language: Lua (lua)
If Statements
If statements will allow you to specify what code to run when a condition is true, and when it isn’t.
If
Use the keyword if
with your condition, followed by the keyword then
. The conditional code will be indented, and finished with the keyword end
.
Examples:
if true then
print("This code will always run")
end
local a = 9
if a < 10 then
print("Less than 10!")
end
Code language: Lua (lua)
Else
You add an else
keyword before the end
of an if-statement to add code that only executes when the if condition is false.
Examples:
if false then
print("This will never run")
else
print("This will instead")
end
if 10 < 100 then
print("This part does run")
else
print("So this part does not")
end
Code language: Lua (lua)
Else If
The else if
keyword before else
lets you evaluate a second condition if the first if condition is false. You can have as many else-ifs as you want. They will be checked in order, so the first one that is true is the one that runs, and no other part of the condition will run.
Example:
local iLikeCoffee = false
local iLikeTea = true
local iLikeJuice = true
if iLikeCoffee then
print("Have some coffee!")
elseif iLikeTea then
print("Have some tea!")
elseif iLikeJuice then
print("Have some juice!")
else
print("Try some water?")
end
Code language: Lua (lua)
The last question of the Lua Conditions quiz was very badly worded
“Between the condition of an if statement, you find the keywords:”
It seems like the question should have been “Around the condition of an if statement you will find the keywords…: “if … then”