Conditions

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 -- 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 variable
local aFalsehood = false -- assigns the value FALSE to a variable

print(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 false

local stringNumber = "7"
local actualNumber = 7

print(stringNumber == actualNumber) -- prints falseCode 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) -- 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 = 3


print(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 = true
local aFalsehood = false

print(aTruth and aFalsehood) -- prints false

local a = 10

print (a > 2 and 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 = true
local iLikeTea = false

print(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 = true
local 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:

if true then
    print("This code will always run")
end

local a = 9

if a < 10 then
    print("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:

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")
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 = false
local iLikeTea = true
local iLikeJuice = true

if iLikeCoffee then
    print("Have some coffee!")
elseif iLikeTea then
    print("Have some tea!") -- This line will run, because it has the first condition that evaluates to TRUE
elseif iLikeJuice then
    print("Have some juice!")
else
    print("Try some water?")
endCode language: Lua (lua)
1 Comment
Collapse Comments

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”

Leave a Comment

Scroll to Top