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 = 5local b = 7
a == b -- the result is falseCode 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-- assigns the value TRUE to a variablelocal aFalsehood = false-- assigns the value FALSE to a variableprint(aTruth == aFalsehood)
-- evaluates whether these variables have the same value. In this case it evaluates to FALSE.
aTruth = aFalsehood
-- reassigns the variable. Now aTruth is FALSE!print(aTruth == aFalsehood)
-- this will be true! Because both aTruth and aFalsehood are assigned to the value FALSE.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) -- prints falselocal stringNumber = "7"local actualNumber = 7print(stringNumber == actualNumber) -- prints falseCode language:Lua(lua)
Not Equal
You can use the ~= operator to evaluate whether two values are NOT equivalent.
local aTruth = truelocal aFalsehood = falseprint(aTruth ~= aFalsehood) -- will print 'true'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) -- prints 'true', because 10 is greater.print(1 < -3) -- prints 'false' because 1 is not less than -3Code 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 = 3print(playersConnected >= 3)
-- prints 'true'. We want to know if there are AT LEAST 3 players connected.print(playersConnected <= 16)
-- prints 'true'. Could use this to make sure that we haven't exceeded a maximum number of playersCode 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 = truelocal aFalsehood = falseprint(aTruth and aFalsehood) -- prints falselocal a = 10print (a > 2and a < 20) -- prints true, because a is between 2 and 20Code 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 = truelocal iLikeTea = falseprint(iLikeCoffee or iLikeTea) -- true, if you like coffee, tea, or bothCode language:Lua(lua)
Not
You can use the keyword not to get the opposite of the truth value.
Examples:
local iLikeCoffee = truelocal iDontLikeCoffee = not iLikeCoffee
print(iDontLikeCoffee) -- false, because I DO like coffeeCode 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:
iftruethenprint("This code will always run")
endlocal a = 9if a < 10thenprint("Less than 10!")
endCode 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:
iffalsethenprint("This will never run")
elseprint("This will instead")
endif10 < 100thenprint("This part does run")
elseprint("So this part does not")
endCode 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 = falselocal iLikeTea = truelocal iLikeJuice = trueif iLikeCoffee thenprint("Have some coffee!")
elseif iLikeTea thenprint("Have some tea!") -- This line will run, because it has the first condition that evaluates to TRUEelseif iLikeJuice thenprint("Have some juice!")
elseprint("Try some water?")
endCode 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”
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!
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”