Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Difference between else and else if?

I had a question about the performance differences of else and else if. I'm under the assumption that if you had something like:

local variable = false
if variable then
	print("True")
elseif not variable then
	print("False")
end

would work (and it would). My question is however, would it be more efficient and save resources on your server if you did:

local variable = false
if variable then
	print("True")
else
	print("False")
end

I am asking, since it would make sense in my mind. The 1st example would take time to process the variable again and check if it's not true. While the 2nd example would regardless of the value after the first check, fire "False". What is more efficient? Is there a noticeable difference?

Hope the question is clear.

Thanks for the help!

1 answer

✓ Accepted answer
else

Is just like If "this" doesnt happen then "this" will happen

if 1 ~= 1 then -- 1 does equal 1 so this is wrong
	print("Correct")
else
	print("no u") -- this will print because "1 ~= 1" is not true
end

So if were to have 3 moves

local FireMoves = {"Move1", "Move2", "Move3"}
local Move = "Move1"
If FireMove[2] == Move then
	print(Move)
else -- this is saying that if the 1st one is false then `else` will happen
	print(Move, " ~= ", FireMove[2])
end

If you were to use

elseif 

you could specify

local FireMoves = {"Move1", "Move2", "Move3"}
local Move = "Move1"
If FireMove[2] == Move then
	print(Move)
elseif FireMove[1] == Move then it specifies another parameter
	print(Move, " = ", FireMove[1])
end

3 comments

1

I'm aware of how they work. I'm asking about if you only had in a hypothetical situation, move1 and move2 (true and false), would the elseif even be necessary? Would using else rather than elseif in that situation save resources? Should that even be a concern with something this small?

0

Oh yea in the 1st part i answered that "If "this" doesnt happen then "this" will happen" you could substitute that for true and false "if "true" doesnt happen then "this" will happen

1

elseif wont be necessary in other words

This discussion is preserved as it was. New answers, comments, and votes are closed.