When the select case statement is encountered, the SelectExpr is evaluated. Then execution proceeds with the statements following the first case statement with a CaseList that matches the value of the SelectExpr. If no matching CaseList is found, then execution proceeds with the statements following the case else statement, if present. Whichever group of statements is selected, after they are executed, execution continues with the statements following the end select statement.
Any number of case statements may appear within a select case statement block, but when the program is run, if no case statement is selected and there is no case else, then an error will occur.
The CaseList is a comma separated list of CaseExprís. The SelectExpr is compared with each CaseExpr in order from left to right until a match is found whereupon the statements which follow are executed. If the CaseExpr list is exhausted and no match is found, then the CaseList itself does not match and execution proceeds as described above.
A CaseExpr may have any of three syntactic forms, but each form is equivalent to a conditional expression containing SelectExpr. If this condition is true, then the CaseExpr is a match, otherwise it is not. The first form of a CaseExpr is simply "Expr" which is equivalent to the conditional expression "SelectExpr = Expr." The second form of CaseExpr is "LoExpr TO HiExpr" which is equivalent to the conditional expression "LoExpr <= SelectExpr AND SelectExpr <= HiExpr." The final form of CaseExpr is "IS RelOp Expr" which is equivalent to the conditional expression "SelectExpr RelOp Expr" and Relop is any relational operator: less than, greater than, equal to, or their logical opposites.
print "Congratulations! You have been selected by the" print "Smarmy Marketing Research Corporation of America" print "as the quintessential case study in Middle America!" print input "What is your age"; a print select case a case is < 5 print "Buy anything you can reach!" case 5 to 11 print "Buy figurines of television characters!" case 12 to 17, 19 to 22 print "Buy popular products to show you're popular!" case 18 print "Buy sex, drugs and rock and roll!" case 23 to 30 print "Buy diapers and aspirin!" case 31 to 45 print "You are in hock to your eyeballs!" case 40 to 55 print "Buy fiberous cereal and deodorants!" case 55 to 65 print "Buy vitamins and hair tonic!" case else print "Sell everything and get on Medicaid!" end selectWhen run, if the user enters "40" will produce the following:
Congratulations! You have been selected by the Smarmy Marketing Research Corporation of America as the quintessential case study in Middle America! What is your age? 40 You are in hock to your eyeballs!
from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber