Question/Discussion about elseif

Hello, all.

I was recently wondering why there’s special syntax for elseif when you could just put an if statement inside of an else statement.
This:

if a = b then
elseif a=c then
end

seems to do the same thing as this:

if a=b then
else
if a=c then
end
end

Any ideas?
Thanks.

Using elseif instead of the alternative method you showed is easier to read and shorter to type, like a shortcut.

2 Likes

If you use the second version:

then it might effect the performance of the CPU.

That’s what I’ve heard/seen on here.

The second one is better of course depending on the situation.

1 Like
if a == b then
    --
else
    if a == c then
        --
    else
        if a == d then
            --
        else
            --
        end
    end
end

This is just confusing to look at… It unnecessarily nests conditions which should be considered in the same scope. I’m sure there are use cases for this, but generally speaking there are cleaner alternatives.

2 Likes
if a == b then
else
	if a == c then
	end
end

What if a is d? Will u do:

if a == b then
else
	if a == c then
	else
		if a == d then
		end
	end
end

How readable is that compared to:

if a == b then
elseif a == c then
elseif a === d then
end

It’s all about readability. There’s no significant difference in performance.

1 Like

Because else refers to literally anything else than the other if statement

local myString = 2

if myString == "2" then
 -- code will not be executed
else 
-- literally anything else that is not myString = "2", since myString equals to only 2, the number, this part will be executed
end

elseif makes sure that another condition is met but doesn’t execute neither if the statement is false

local myString = 2

if myString == "2" then
 -- code will not be executed
elseif myString == 2 then
-- makes sure another condition is met, if false, it skips. This code will be executed
else
-- this code will not be executed since the elseif statement is true
end
1 Like

Well… that part wasn’t really the point. But good question!

This is about elseif and else statements with an if statement inside. Not else, my friend!

I’ve provided evidence why else is eh and using an elseif is in general better if you want more accuracy. So you would prefer something like this?

if false then
	
else
	if false then

	else
		if false then

		else
			if false then

			else
				if false then

				else
					if false then

					else
						if false then

						else
							if false then

							else
								if false then

								else
									if false then

									else
										if false then

										else
											if false then

											else
												if false then

												else
													if false then

													else
														if false then

														else
															if false then

															else

															end
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

And also your question was this

It’s so you can avoid the pyramid of doom.
image