BB___SELECT_CASE

=SELECT..CASE= toc

Description
> This is a decision-making structure. Based on the result of evaluating a variable or expression against a //set// of conditions, it controls which statement or statements the program will execute next.

GENERAL
> Given some input for evaluation, it tests the input against each CASE value in order. If a CASE meets the condition, (if it evaluates to TRUE), the statements following that CASE are executed. If no conditions are met, and a CASE ELSE statement is present, the statements following the CASE ELSE will be executed. The structure ends with the END SELECT statement.

> The //expr// can be omitted from the SELECT CASE statement and the CASE statements can be evaluated individually against a named variable or expression that has been defined elsewhere in the program. See the example for Multiple Case Conditions.

> SELECT CASE statements can be nested.

> See also IF..THEN.

Syntax
> //There are three general forms a SELECT CASE statement can take.//

Basic Select Case
> //Evaluating a numeric or string variable or expression//. >> case //value X// >>> //statement(s) to execute// >> case //value Y// >>> //statement(s) to execute// >> case //value Z// >>> //statement(s) to execute// >> case //else// >>> //statement(s) to execute// > end select
 * 1) select case //expr//

Multiple Values for CASE
> //More than one CASE value can be evaluated when separated by commas//. >> case //values a, b, c// >>> //statement(s) to execute// >> case //values d, e, f// >>> //statement(s) to execute// >> case //values g, h, i// >>> //statement(s) to execute// >> case //else// >>> //statement(s) to execute// > end select
 * 1) select case //expr//

Multiple Case Conditions
> //More than one Condition can be evaluated in a SELECT CASE structure//. >> case //condition to evaluate// >>> //statement(s) to execute// >> case //condition to evaluate// >>> //statement(s) to execute// >> case //condition to evaluate// >>> //statement(s) to execute// >> case //else// >>> //statement(s) to execute// > end select
 * 1) select case

Hints
> Any number of conditions can be evaluated. The testing ends with the first CASE that satisfies the test condition, even if subsequent CASE statements are also true.

> Code to be executed in a CASE block can also include goto [branchLabel], gosub [branchLabel], or a call to a sub or function. When the program returns from a subroutine initiated by a call or a gosub, the statement following the END SELECT is the next statement to be executed, since the structure has finished evaluating its conditions.

> In JB, //condition to evaluate// will result in a boolean value. With the IF statement, any non-zero integer is True, but CASE accepts only 1 as True. All other values work as False. That means using NOT function in condition will not work (very likely bug in SELECT CASE).

Basic Select Case
code format="lb" num = 3 select case num case 1 print "one" case 2 print "two" case 3 print "three" case else print "other number" end select

' In this basic Case use, the variable, num, is tested against the value ' supplied with each CASE. The condition being tested is: "What CASE ' value is the same as num?" The CASE statement that has a value of "3" ' meets the test; it is TRUE. The statement following is executed and ' "three" is printed.

' A short example with a string variable gender$ = "Male" select case gender$ case "Male" print "You are a Paladin" case "Female" print "You are a Sorceress" case else print "You are a Dwarf" end select

' prints "You are a Paladin" code

Multiple Values for Case
code format="lb" ' Evaluating multiple string cases. In this example, the variable, ' mo$, is compared to any of the three string values following a ' CASE statement.

input "Enter the current month "; mo$

' Use first 3 letters of user's input and convert to caps. mo$ = upper$(left$(mo$, 3))

select case mo$ case "DEC", "JAN", "FEB" season$ = "Winter" case "MAR", "APR", "MAY" season$ = "Spring" case "JUN", "JUL", "AUG" season$ = "Summer" case "SEP", "OCT", "NOV" season$ = "Fall" case else print "Invalid month entered." end end select

print "It is the "; season$; " season." end code

Multiple Case Conditions
code format="lb" ' Evaluating multiple conditions. When an expression is not ' included in the SELECT CASE statement, the individual CASE ' conditions can be evaluated against a named variable or expression.

' In this example, "num" is the named numeric variable that will be ' evaluated by expressions in each of the CASE statements. The ' objective is to find which CASE has the condition that evaluates to ' a value equal to the value of "num".

input "Word to categorize: "; wd$ ' Convert to lower case wd$ = lower$(wd$) ' Get ASCII value of 1st letter of wd$ num = asc(wd$)

select case case (num > 96) and (num < 104) print "Category a - g"       case (num > 103) and (num < 111) print "Category h - n"       case (num > 110) and (num < 119) print "Category o - v"       case (num > 118) and (num < 123) print "Category w - z"       case else print "Invalid entry" end end select

end ' Prints the Category range in which the input word would fit. code

Useful Functions
code format="lb" Place a useful function using this command here code