Liberty BASIC Help Online

SELECT CASE
 
Description:
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"
        print "red"
    case "green","yellow"
        print "green or yellow"
    case else
         print "color unknown"
end select
 
 
MULTIPLE CASES - may be evaluated when separated by a comma.
For example:
 
  select case a+b
    case 4,5
      do stuff
    case 6,7
      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".
 
'incorrect usage if multiple cases must be evaluated:
select case value
 
 
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
 
 
See also:  if...then


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