Which is better and what are the differences between
1 | if bool = = true then |
2 | print ( "Hello World!" ) |
3 | else -- Else |
4 | print ( "Goodbye World" ) |
5 | end |
or(and)
1 | if bool = = true then |
2 | print ( "Hello World!" ) |
3 | elseif -- Elseif |
4 | print ( "Goodbye World" ) |
5 | 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?
"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:
1 | if hi = = 1 then |
2 | print ( "k" ) |
3 | elseif hi = = 2 then |
4 | print ( "ok" ) |
5 | 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:
1 | if bool = = true then |
2 | print ( "Hello World!" ) |
3 | elseif bool = = false then |
4 | print ( "Goodbye World" ) |
5 | end |
But since this value type is a boolean, it's more efficient to do this:
1 | if bool = = true then |
2 | print ( "Hello World!" ) |
3 | 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. |
4 | print ( "Goodbye World" ) |
5 | 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.
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:
1 | if value = = 5 then |
2 | print "yay its 5!" |
3 | else |
4 | print "not 5" |
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."
1 | if value = = 5 then |
2 | print "yay its 5!" |
3 | elseif value = = 4 then |
4 | print "meh, its 4" |
5 | 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:
1 | if value = = 5 then |
2 | print "yay its 5!" |
3 | else |
4 | if value = = 4 then |
5 | print "meh, its 4" |
6 | end |
7 | 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.
1 | if value = = 5 then |
2 | print "yay its 5!" |
3 | elseif value = = 4 then |
4 | print "meh, its 4" |
5 | else |
6 | print (value.. " is not good enough!" |
7 | end |
You can read more on conditional statements here.
I hope this answer helped you. If it did, please upvote and accept!
Well Its Pretty Simple!
1 | b = 1 |
2 |
3 | if b = = 1 then |
4 | print ( "HI" ) |
5 | elseif b = = 0 then |
6 | print ( "Bye" ) |
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?