Default
Select <.expression.> ... Case <.expression.> ... Default ... EndSelect
 
Parameters: NONE
Returns: NONE
 
     Select / Case statements are an alternative to If / Then statements when you have a large number of conditions to examine.

      The simplest form of a Select / Case block looks like this

  
  myInt = 1
; myInt = 2
; myInt = 3
  Select myInt
      Case 1
          Print "The first case"
      Case 2
          Print "The second case"
  EndSelect
  Sync
  WaitKey
  


      If the variable myInt equals 1 this example will output The first case if myInt equals 2, it will output The second case. All other values of myInt will cause no output at all. In order to process all cases that are not explicitely stated with the Case keyword, you can use the keyword Default:

  
; myInt = 1
; myInt = 2
  myInt = 3
  Select myInt
      Case 1
          Print "The first case"
      Case 2
          Print "The second case"
      Default
          Print "myInt is neither 1 nor 2"
  EndSelect
  Sync
  WaitKey
  


     This example would output myInt is neither 1 nor 2 if the value of myInt is less than 1 or greater than 2.

     You can also check for value ranges with in the case statements by using the keyword To.

  
  myInt = 1
; myInt = 42
  Select myInt
      Case 1 To 2
          Print "myInt is either 1 or 2"
      Case 10 To 100
          Print "myInt is greater than 9"
          Print "myInt is smaller than 101"
      Default
          Print "The value myInt is neither 1 nor 2"
          Print "nor between 10 or 100"
  EndSelect
  Sync
  WaitKey
  


     If certain conditions apply to a number of unrelated values, you can list these values after the case keyword, separated by commas:

  
  myInt = 42
  Select myInt
      Case 144278
          Print "It's either 14 42 or 78"
      Case 1 To 10
          Print "It's between 1 and 10"
      Default
          Print "Neither of the above"
  EndSelect
  Sync
  WaitKey
  


     By default PlayBASIC will exit a Select/EndSelect block as soon as a case match occurs. In order to force PlayBASIC to check further cases, use the ContCase statement:

  
; myInt = 14
  myInt = 42
; myInt = 178
  Select myInt
      Case 1442178
          Print "It's either 14, 42 or 178"
      ContCase
      Case 1 To 100
          Print "It's between 1 and 100"
  EndSelect
  Sync
  WaitKey
  




     Select/Case statements work with all standard types, ie. Integer, Float and String. Opposed to a lot of other Basic dialects, PlayBasic also accepts expressions after the Case keyword.

     The expression followed by the Select keyword must be of the same type as the expressions or constants followed by the Case statements. Otherwise PlayBasic will return an error.


 
Related Info: Case | ContCase | EndSelect | if | Select :
 


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