Decisions Part I

Objective

  • Understand how Lua makes decisions

  • Understand which lines of code get executed when a decision is made

Conditional Statement

Conditional statements perform different actions or computations depending on whether a boolean condition is true or false.

Relational Operators

A relational operator determines whether a specific relationship exists between two values.

Boolean Expression

A Boolean expression tests if the conditional statement is true or false.

Relational Operators

Symbol

Meaning

<

less than

<=

less than or equal to

>

greater than

>=

greater than or equal to

==

equal

~=

not equal

Boolean Expression using a Relational Operator

Boolean Expression

Meaning

x < y

x less than y

x <= y

x less than or equal to y

x > y

x greater than y

x >= y

x greater than or equal to y

x == y

x equal to y

x ~= y

x not equal to y

if Statement

The if conditional statement can be true or false. When the if statement executes, the condition is tested and if true, the code is executed. Otherwise it is skipped.

Example 1

-- equality example
x = 2
if x == 2 then
print("x is equal to 2")
end

Example 2

-- greater than or equal example
x = 1
if x >= 0 then
print("x is greater than or equal to 0")
end

Example 3

-- not equal example
x = 2
if x ~= 1 then
print("x is not equal to 1")
end

Common Mistake

-- Common Mistake!
if x = 1 then
print("Why is this a mistake? ")
end

if else Statement

The if conditional statement can be true or false. When the if statement executes, the condition is tested and if true, the code is executed. If the condition tested is false, the else code is executed.

Example 1

io.write("What's your favorite Roblox game? ")
game = io.read()
if game == "Meep City" then
print("we love playing Meep City")
else
print("how about Adopt Me?")
end

Example 2

io.write("How much Robux do you have? ")
robux = io.read("*number")
if robux > 100 then
print("You have enough to buy a hat")
else
print("Time to earn more Robux!")
end