ELSEIF
If [Expression=true] ..Run This Code Elseif [Expression=true] run this code.... Endif
 
Parameters: NONE
Returns: NONE
 

      ELSEIF is used in combination with the conditional If/EndIf and If/Else/EndIf statements. Unlike Else However, ELSEIF can perform comparison also.

      If performs a comparison upon the result of an expression, if the expression resolves to be true, the code following the If statement and between the Else statements will be executed. If not, the code between the ElseIf-Endif statements will be executed.

      For more information on If/EndIf statements please refer to the If page.




FACTS:


      * ELSEIF can only be used inside IF/ENDIF statements

      * ELSEIF can not follow an ELSE statement.

      * There is no limit to the number of ELSEIF that can be used within a IF/ENDIF statements.





Mini Tutorial:



      Previously we've seen that the If / Else / EndIf combination, allows us to selectively execute code based upon whether the If condition is TRUE or FALSE.

      While If / Else / EndIf can be very powerful, sometimes we'll need even more control than it allows. However when the IF expression is FALSE, we might need to perform another IF comparison to find the match we're after. As a result, we end up nesting If / EndIF statements.

      First, lets look at an example of this. Bellow i've nested three IF/Endif statements inside each other. The code is trying catch and execute the correct response depending upon what value is stored in the 'Weapon' Variable.

Example

  
; Set the Weapon variable to 2
  Weapon = 2
  
; Compare Weapon with 1
  If Weapon =1
     Print "Player is using the Small Gun"
  Else
     
   ; If the previous IF statement was false,
   ; Compare Weapon With 2
     If Weapon =2
        Print "Player is using the Big Gun"
     Else
        
      ; If the previous IF statement was false,
      ; Compare Weapon With 3
        If Weapon =3
           Print "Player is using the Massive Gun"
         ;end of the first if statement
        EndIf
        
      ; end of the second if statement
     EndIf
     
   ;end of the first if statement
  EndIf
  Print "Done"
  Sync
  WaitKey
  


      This example would output the following (bellow) when executed. Now since the Weapon variable equals 2, the first IF statement we hit will be FALSE as Weapon does no equal 1. Because it's False, execution will jump to the ELSE statement of the first IF. Here it will perform another IF comparison. This time the if statement will be TRUE as Weapon does indeed equal 2. Because the condition is met. Execution will run the code following that IF, then it'll jump out to that If statement to it's closing/matching EndIF statement.

      So in this example the 3rd IF statement, which has been nested inside the second If statement. Is Never even considered.

  
  Player is using the Big Gun
  Done
  


      While that's a perfectly acceptable approach, It can be a tad confusing to follow. This is where the ElseIF statement can make our code much clearer to read

     ElseIf acts much like the Else statement, where if the opening If[ condition is FALSE, execution skips ahead to the ElseIF statement like we've expect. But ElseIf can also evaluate a conditional expression just like the If statement. This Allows us to test multiple the TRUE/ FALSE conditions, without having to nest If/EndiF statements. You can can have as many ElseIF statements in a row as you like. When your finished, you close the statement with a single EndIF

Example

[code]
; Set the Weapon Variable to 2
Weapon = 2

; Compare Weapon with 1
If Weapon =1
Print "Player is using the Small Gun"

; If the previous IF statement was false,
; Compare Weapon With 2
ElseIf Weapon =2
Print "Player is using the Big Gun"

; If the previous IF statement was false,
; Compare Weapon With 3
ElseIf Weapon =3
Print "Player is using the Massive Gun"

; close this IF statement
EndIf
Print "Done"

Sync
WaitKey
[/code]

      This example would output the following (bellow) when executed. Now since the 'Weapon' variable equals 2, when we hit the IF statement, the comparison will be FALSE since Weapon does not equal 1. Since it's False, execution will jump to the first ElseIF statement. Here it will perform another comparison, comparing Weapon with 2 this time. This statement will be TRUE, so the code following this ElseIF is executed. Once execution is complete, it will jump out of and over any following code to the closing EndIF statement.

      So just like the previous example the Second ElseIF statement Is Never even considered.

  
  Player is using the Big Gun
  Done
  



 
Related Info: Case | ELSE | ENDIF | IF | Select :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com