What is the difference between Elseif and Else

Post » Sun May 01, 2011 6:41 pm

Is there any major differences between the two? When should i use else instead of elseif????
User avatar
Robert DeLarosa
 
Posts: 3415
Joined: Tue Sep 04, 2007 3:43 pm

Post » Sun May 01, 2011 3:20 pm

The two are related, think of an elseif as an else with a condition. Taking the following example.

if ( x == 1 )    Code here is executed when x == 1elseif ( x == 2 )    Code here is executed when x is not 1 and x == 2else    Code here is executed in all other cases. In other words x is not 1 and x is not 2endif



Some examples in English
If it is Tuesday we should go shopping else if it is Wednesday we could go to the beach

Becomes
if ( day == Tuesday )    go shoppingelseif ( day == Wednesday )  go to the beachendif

In this case, we go shopping if it is a Tuesday and go to the beach if it is a Wednesday, but there is no case for any other day

If it is sunny we could go to the beach else we will go shopping
if ( weather == sunny )    go to the beachelse    go shoppingendif

In this case, if the weather is sunny we go to the beach and in all other cases we go shopping. The else will always happen if none of the conditaions in the if or elseif statments are satisfied.


else could also be considered the same as elseif ( 1 == 1 ). 1 is always equal to 1 so the code in the elseif block will always be executed if none of the above conditions are met.

Some other quick examples

if ( x == 1 )elseif ( x == 1 )    code here is never executed as there is already a condition that is satisfied above this oneelse    code here is executed when x is not equal to 1endif


if ( x == 1 )    code here is executed when x == 1elseif ( y == 1 )    code here is executed when y == 1 and x != 1elseif ( z == 1 )    code here is executed when z == 1 and x != 1, y != 1else    code here is executed when x != 1, y != 1 and z != 1.endif

User avatar
Joie Perez
 
Posts: 3410
Joined: Fri Sep 15, 2006 3:25 pm

Post » Sun May 01, 2011 5:09 pm

Thanks! I had some people on here help me with some scripts i was trying to write for my mod, and i noticed some of them were inccorrect by the way I worded it I gues you can say. I basically had to many if statements in some of the scripts and started using elseif's and it seemed to help. That helps so basically if i'm doing an else statement there isn't a condition set for it?

Like i will always have ( put in here what i want ) these after an if and elseif but not an else

if ( condition )

elseif ( condition )

else


Also, if statements are only executed if the condition is true. An Elseif is only executed if the if condition from above was false and IF the condition within the statement of the elseif is true. An else command will executed no matter what as long as all the above is false.

Is that right? I think i got it.

My question if i'm writing scripts what if i write a script let's say i have this

If ( condition )

blah blah

elseif ( condition )

blah blah

OK, let's say if the first condition is false and the elseif condition is false. Will the script continously run? Is this not usually a good thing? So if I do a if and elseif condition i should usually do it for a situation that can ONLY be either this or that. Otherwise i should use the else command?
User avatar
Michelle Serenity Boss
 
Posts: 3341
Joined: Tue Oct 17, 2006 10:49 am

Post » Sun May 01, 2011 3:18 pm

Also, if statements are only executed if the condition is true. An Elseif is only executed if the if condition from above was false and IF the condition within the statement of the elseif is true. An else command will executed no matter what as long as all the above is false.
Correct.

OK, let's say if the first condition is false and the elseif condition is false. Will the script continously run? Is this not usually a good thing? So if I do a if and elseif condition i should usually do it for a situation that can ONLY be either this or that. Otherwise i should use the else command?
What do you mean by "continuously run"? *Every* script continuously runs in the sense that it runs every frame unless stopped by StopScript (and unless it's an attached script that can't be StopScript'ed and only stop when their respective items are SetDelete'd out of the world; Disable isn't sufficient). If none of conditions matched now, the next frame they will be checked again. They won't be checked repeatedly in the *same* frame, if you meant that.

If none of if's/elseif's matches, and there's no else, simply nothing happens.
User avatar
Shannon Lockwood
 
Posts: 3373
Joined: Wed Aug 08, 2007 12:38 pm

Post » Mon May 02, 2011 1:55 am

When you can use else rather than elseif, I recommended you just use the else. The main benefit of is that it can save you if the variable values go out of the expected range. Always expect the unexpected, as other players and their machines will do their best to break your script. :)
User avatar
Harinder Ghag
 
Posts: 3405
Joined: Wed Jan 17, 2007 11:26 am


Return to III - Morrowind