Liberty BASIC Help Online

Select Case Statement
 
Many BASICs have a Select Case statement, and Liberty BASIC 3 adds that capability also.  It is a good alternative when many possible conditions must be evaluated and acted upon.  The Select Case construction also provides a Case Else statement for implementing a routine when the evaluated condition meets none of the cases listed.  For more, see SELECT CASE.
 
 
SELECT CASE is a construction for evaluating and acting on sets of conditions.  The syntax for Select Case is:
 
SELECT CASE var
    CASE x
        'basic code
        'goes here
    CASE y
        'basic code
        'goes here
    CASE z
        'basic code
        'goes here
    CASE else
        'basic code
        'goes here
    END SELECT
 
Details:
 
SELECT CASE var  - defines the beginning of the construct.  It is followed by the name variable that will be evaluated.  The variable can be a numeric variable or a string variable, or an expression such as "a+b".
 
CASE value - following the SELECT CASE statement, are individual CASE statements, specifying the conditions to evaluate for the selected variable. Code after the "case" statement is executed if that particular case evaluates to TRUE.  There is no limit to the number of conditions that can be used for evaluation.
 
CASE ELSE - defines a block of code to be executed if the selected value does not fulfil any other CASE statements.
 
END SELECT - signals the end of the SELECT CASE construct.
 
Example usage:
 
num = 3
 
select case num
    case 1
        print "one"
    case 2
        print "two"
    case 3
        print "three"
    case else
        print "other number"
    end select
 
The example above results in output in the mainwin of:
 
three
 
 
Strings
SELECT CASE can also evaluate string expressions in a similar way to numeric expressions.
 
String example:
 
var$="blue"
 
select case var$
    case "red"
        do stuff
    case "green","yellow"
        do stuff
    case else
         do stuff
end select
 
 
MULTIPLE CASES - may be evaluated when separated by commas.
For example:
 
  select case a+b
    case 4,5
      do stuff
    case 6,7,8
      do other stuff
  end select
 
Once one of the CASEs has been met, no other case statements are evaluated.  In the following example, since the value meets the condition of the first CASE statement, the second CASE statement isn't considered, even though the value meets that condition also.
 
num = 3
 
select case num
    case 3, 5, 10
        print "3, 5, 10"
    case 3, 12, 14, 18
        print "3, 12, 14, 18"
    case else
        print "Not evaluated."
end select
 
The example above results in output in the mainwin of:
 
3, 5, 10
 
Evaluating multiple conditions in the CASE statement
Omitting the expression (or variable) in the SELECT CASE statement causes the conditions in the CASE statements to be evaluated in their entirety.  To omit the expression, simply type "select case" with no variable or expression after it.  In the following example, since "value" evaluates to the first CASE statement, the printout says "First case"
 
'correct:
value = 58
 
select case
    case (value < 10) or (value > 50 and value < 60)
        print "First case"
 
    case (value > 100) and (value < 200)
        print "Second case"
 
    case (value = 300) or (value = 400)
        print "Third case"
 
    case else
        print "Not evaluated"
end select
 
If the expression "value" is placed after "select case", then none of the CASE statements is met, so CASE ELSE is triggered, which prints "Not evaluated".  The expression must be omitted to evaluate multiple values in a SELECT CASE statement:
 
'wrong:
select case value
 
'correct:
select case
 
 
Nested statements
Nested select case statements may be used.  Example:
 
  select case a+b
    case 4,5
      select case c*d
        case 100
          do stuff
      end select
      do stuff
    case 6,7
      do other stuff
  end select


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/