Jump to content

Why use ElseIf?


Leemonski

Recommended Posts

I've noticed that I don't really need to use ElseIf statements. Whats the difference between these two examples?

 

Begin GameMode

    if a == 1
         set b to 1
    endif

    if a == 2
         set b to 2
    endif

End

 

Begin GameMode

    if a == 1
         set b to 1

   elseif a == 2
         set b to 2
    endif

End

 

These two examples would work exactly the same way right? So why use elseif at all?

Link to comment
Share on other sites

Think there is no essential difference. you just spare the endif so you'll have a better organisation if you use many conditions.

Begin GameMode

    if a == 1
         set b to 1

   elseif a == 2
         set b to 2

   elseif a == 3
         set b to 3

              if c == 0
                  set c to 1
              else
                  set c to 0
              endif

   elseif a == 4
         set b to 4

    endif

End

Link to comment
Share on other sites

Elseif statements can also be used as a means of restricting what result is used due to the logic involved.

 

if a >= 12
  <stuff>
elseif a >= 5 
  <stuff>
endif

compared with

if a >= 12
  <stuff>
endif

if a >= 5 
  <stuff>
endif

If A were a value higher than 12:

In the first one, it's treated as a single statement, so only the first block will be used . In the second one, you have two statements so both blocks of stuff would be used.

 

When combined with a generic else statement, it refines the logic even further;

if a >= 12
  <stuff>
elseif a >= 5 
  <stuff>
else
  <stuff>
endif

Meaning that if A is greater than 12, the first block of stuff is used, if A is between 5 and 12, the second block of stuff is used, if A is less than 5, the third block is used.

 

Additionally, in particularly long scripting, using elseif instead of a separate statement can be used to make sure that related parts are checked at roughly the same time, and that logic over what parts within the scripting are being used at any time can be controlled to work only when you want them to.

Link to comment
Share on other sites

I've noticed that I don't really need to use ElseIf statements. Whats the difference between these two examples?

 

Begin GameMode

if a == 1
set b to 1
endif

if a == 2
set b to 2
endif

End

 

Begin GameMode

if a == 1
set b to 1

elseif a == 2
set b to 2
endif

End

 

These two examples would work exactly the same way right? So why use elseif at all?

 

They are not exactly the same. In your particular example, it will achieve the same effect... BUT, the elseif version does so faster/more efficiently. With elseif, if the first if condition is true then the script does not bother evaluating the second "elseif a==2" - the script executes just a little bit faster.

 

In a different example

 

Begin GameMode

if a == 1
set a to a+1
endif

if a == 2
set a to a+2
endif

End

 

With this example, if a starts out at 1 then a winds up being equal to 4.

 

Begin GameMode

if a == 1
set a to a+1

elseif a == 2
set a to a+2
endif

End

 

With this one, if a starts out at 1, then a winds up equal to 2.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...