I recently started to learn about Embedded If Statements and no matter how much I watch them I get confused. Is Embedded If Statements A Elseif?
An embedded if statement is just almost a recursion of if statements:
if A then
if B then
print("This is an embedded statement, dependency on A.")
end
print("First statement is valid.")
else
print("The other statement is valid.")
end
Just think of an embedded if statment, as an if statment inside of an if statment.
if true then --first if statment --pass the first condition
print("I passed first if statment")
if true then --the 2nd one, aka the embedded one --then pass the 2nd condition
print("I passed second if statment")
end
end
You can have as many embedded if statments as you want, you can littearly have an if statment inside of an if statments which is also inside of an if statment, and it continues
if true then
if true then
if true then
end
end
end --a chain of ends!!
Embedded if statments are useful, when you need to check 2 conditions, and not at the same time, meaning without using the and
operator, which I recommend you check out.
Like if you wanted to check if a part’s brickcolor was red, but what if the object you were checking was not a part, that will result into an error, meaning you also have to check if the object is a part, using :IsA()
.
if object:IsA("Part") and object.BrickColor == BrickColor.new("Really Red") then --using and, to check for two things
end
But even if the object wasn’t a part, and the condition was not met, this would still error! Because we’re also checking at the same time for the colour, and we’re doing object.BrickColor
, if the objetc wasn’t a part it wouldn’t have a brickcolor property, thus it errors. That’s why we have to check them seperately, so we check if it’s even a part first, then check if it’s brickcolor is red, and proceed
if object:IsA("Part") then
if object.BrickColor == BrickColor.new("Really red") then
--do something
end
end
Oh and also if you’re wondering, else
s and elseif
s that are attached to the first/original if statment aren’t considered embedded, beacuse they are a part of the if statment as a whole.
if true then
if true then --embedded
else --part of the embedded
end
elseif true then --not embedded, it's a part of the first statment
else
end
I believe what @Operatik is saying is correct. An embedded if statement would be an if statement within another if statement (embedded). Like shown above, the statement is not the same as if A and B
because code can run if A
and separate code can run if A and B
. Similarly, the else statement will only run if A evaluates to false, whereas with if A and B
it would run if not A or not B
.
P.s. Here’s some tricks you can use with and
and or
:
local A = B or C -- or/and will actually not become true/false but will become the value. The only values which evaluate to false are false, and nil.
-- A will be B if B is neither false nor nil. Otherwise if B evaluated to false A will be equal to C. This even works even if C is nil. A will also be nil, but if C is false A will be false too.
-- This can be used like the below statement:
local A = condition and value -- If condition is nil or false, A will be nil or false, otherwise whatever value is.
-- Because nil and false both evaluate to false you can have condition be nil and get one result, or false and get another.
if A == false then -- You can also specify nil and it'll have the same result but the code for nil will be swapped
print("We got false!")
elseif not A then
print("We got nil!")
end
-- This lets you for example, signal an error:
function myFunction(someValue)
if not someValue then
return false
end
return script:FindFirstChild(someValue)
end
local value = myFunction("MaybeAModule")
if value == false then
print("Uh oh! The function returned false at first call")
elseif value then
print("Found the instance!", value)
end
local value = myFunction(nil) -- We could also pass false, and even handle that in the function, for example, to signal that the error should be ignored
if value == false then
print("Uh oh! The function returned false at second call")
elseif value then
print("Found the instance!", value)
end
By Embedded, do you mean in-line ‘if’ statements?
In-line
local a = 1
local b = 2
local c = a == 1 and a or b -- returns 1
-- So, if 'a' is equal to the number 1, c should equal a.
local d = a > b and a or b -- returns 2
-- In this case, if 'a' is greater than 'b' (which it is not), return a, otherwise b.
Embedded ‘if’ statements on the other hand, are just normal if statements within another if statement.
Their are 3 parts to an if statement you can use;
local a = 0
if (a == 0) then
--if 'a' is equal to 0, run the code here
elseif (a == 1) then
--if 'a' is equal to 1, run the code here
else
--if none of the above are true, run the code here.
end
You don’t need to include ‘elseif’ or ‘else’ if you only need to check an alternative outcome.
For example, one use case of this could be with getting a user’s data:
local userData = FunctionThatGetsUsersData() -- so imaginative. lol
if userData ~= nil then
-- if we have user data, do something with it.
end
> CHALLENGES
Here’s a few quick challenges to test your knowledge of IF statements and how they can be applied.
Please solve these questions, and then show the answer to see if you’re correct.
If you missed the mark, try and work back through the statement slowly again.
Challenge 1.
local question = "hey"
local reply = question == "hey" and "hello there!" or "..."
Answer
The answer is ‘hello there!’
Remember, in line if statements always use the first outcome to denote the ‘true’ response, and whatever comes after the ‘or’ is the ‘false’ response.
Challenge 2.
local something = 4
local value = something == 1 and 'Answer 1!' or something == 2 and 'Answer 2!' or 'Answer 3!'
Answer
The answer is ‘Answer 3!’
Like embedded if statements, inline if statements can be stacked!
In this case, ‘something’ did not equal 1 or 2, so the last statement was used.
This is equivalent to:
if something == 1 then
--answer 1
elseif something == 2 then
--answer 2
else
--answer 3
end
Challenge 3.
local a
if (false) then
if (true) then
a = 1
end
elseif (true) then
if (false) then
a = 2
else
a = 3
end
else
a = 4
end
Answer
The answer is ‘3’
This is a slightly more complex statement, but it’d be rare you’d see something like this in production code - many ifs and embedded ifs are bad for performance!
Anyhow, in this challenge, all you have to do is follow the ‘true’ values between each if statement.
If it is false, ignore it!
Let me know if this has been useful to you, and please ask questions if anything isn’t clear!
It is a good tutorial BUT I haven’t learned about Logical Operators and Relational/Conditional Operators but i will learn those things, Thanks anyway
While we’re here, I have to share a story from my roommate this week:
He is in college for computer science. He got an assignment: “Use nested if statements to calculate blah blah blah.” So…he uses nested/embedded if statements. And then he gets a bad grade on it! The professor’s comments were: “It would be better to not use nested if statements for this use case.”
My roommate is upset for good reason. However, interestingly, the professor is still right. While my roommate did the assignment “correctly” according to the rules, the actual application would be better without nested statements.
In most cases, using nested if statements can be avoided by using and
and or
logical operations. But sometimes it’s better to nest them for the sake of readability.
I’m often running into this exact conundrum when programming, especially in lua and in the end, I tend to go for readability, nesteds also more cozy. Not sure if you’d find this of interest at all and not to plug but it goes more in depth.