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
10 years ago

Which is better and what are the differences between

1if bool == true then
2print("Hello World!")
3else -- Else
4print("Goodbye World")
5end

or(and)

1if bool == true then
2print("Hello World!")
3elseif -- Elseif
4print("Goodbye World")
5end

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
10 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:

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

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:

1if bool == true then
2    print("Hello World!")
3elseif bool == false then
4    print("Goodbye World")
5end

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

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

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 — 10y
1
Thank you guys! woodengop 1134 — 10y
2
Thank you, yumtaste. You're welcome, TheContinentofEurope. Redbullusa 1580 — 10y
Ad
Log in to vote
2
Answered by
yumtaste 476 Moderation Voter
10 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:

1if value == 5 then
2print"yay its 5!"
3else
4print"not 5"
5end

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."

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

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:

1if value == 5 then
2print"yay its 5!"
3else
4if value == 4 then
5print"meh, its 4"
6end
7end

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.

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

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 — 10y
1
But...but...I posted my answer first, and mine wasn't accepted... yumtaste 476 — 10y
0
I wish I Could accept both! woodengop 1134 — 10y
0
lolok yumtaste 476 — 10y
Log in to vote
-2
Answered by 10 years ago

Well Its Pretty Simple!

1b = 1
2 
3if b == 1 then
4print("HI")
5elseif b == 0 then
6print("Bye")