Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
6

Elseif or Else? [closed]

Asked by
woodengop 1134 Moderation Voter
9 years ago

Which is better and what are the differences between

if bool == true then
print("Hello World!")
else -- Else
print("Goodbye World")
end

or(and)

if bool == true then
print("Hello World!")
elseif -- Elseif
print("Goodbye World")
end

What are the Purposes for Else and Elseif, Do they have the Same job just different Names, or Does one of them have more Ability than the other?

Locked by Redbullusa and M39a9am3R

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

3 answers

Log in to vote
7
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

"Better" is a relative term. It depends upon what you want to accomplish.

Just a Tip: You know how an "if" statement needs a condition? So does an "elseif".

Example:

if hi == 1 then
    print("k")
elseif hi == 2 then
    print("ok")
end

All right, how to know whether to use an "else" or an "elseif" all boils down to knowing what these terms mean.

  • else - Any condition that's not in the "if" statement.

  • elseif - One specific condition that's not in the "if" statement. Pretty much restating the original "if" statement.

So, regarding your script, an "else" is "better" because there are less characters, therefore less of an eyesore. You could also use an "elseif" statement, but there won't be any point due to the value type of your variable.

Since your variable is a boolean value, there are only two values you can choose: true or false. So if something's true, using "else" means the opposite. For example:

You could do this:

if bool == true then
    print("Hello World!")
elseif bool == false then
    print("Goodbye World")
end

But since this value type is a boolean, it's more efficient to do this:

if bool == true then
    print("Hello World!")
else    -- This next condition HAS to be the opposite of "true" (therefore, "false"), because it's the only option that bool values can give you.
    print("Goodbye World")
end

So you must take into account of the value type and what you want to accomplish in order to choose which of them is "better" for your script.

Any questions/comments/skepticism? Check back with me; we'll sort it out.

1
Nice formatting (not sarcasm). yumtaste 476 — 9y
1
Thank you guys! woodengop 1134 — 9y
2
Thank you, yumtaste. You're welcome, TheContinentofEurope. Redbullusa 1580 — 9y
Ad
Log in to vote
2
Answered by
yumtaste 476 Moderation Voter
9 years ago

Elseif is pretty much what it seems like. It's an else and if statement combined. The difference between elseif and else is kind of hard to explain without showing some code. Here's my best explanation:

if value == 5 then
print"yay its 5!"
else
print"not 5"
end

The code above would print "yay its 5!" if "value" is equal to 5. If the value is anything else, it will print "not 5."

if value == 5 then
print"yay its 5!"
elseif value == 4 then
print"meh, its 4"
end

The above code snippet would print "yay its 5!" if "value" is equal to 5, and print "meh, its 4" if "value" is equal to 4. You could technically do this:

if value == 5 then
print"yay its 5!"
else
if value == 4 then
print"meh, its 4"
end
end

It's generally better to use elseif instead of using "else" and "if" separately, because you don't need another end with elseif, and using elseif is much easier to use if you have multiple conditions.

You can also combine elseif and else.

if value == 5 then
print"yay its 5!"
elseif value == 4 then
print"meh, its 4"
else
print(value.." is not good enough!"
end

You can read more on conditional statements here.

I hope this answer helped you. If it did, please upvote and accept!

1
Thanks guys, Both Answers Explained A lot, +1 Both. woodengop 1134 — 9y
1
But...but...I posted my answer first, and mine wasn't accepted... yumtaste 476 — 9y
0
I wish I Could accept both! woodengop 1134 — 9y
0
lolok yumtaste 476 — 9y
Log in to vote
-2
Answered by 9 years ago

Well Its Pretty Simple!

 b = 1

if b == 1 then
print("HI")
elseif b == 0 then
print("Bye")