MC-Basic:SELECT ... CASE

From SoftMC-Wiki
Jump to: navigation, search
Language: English  • 中文(简体)‎

This decision construct enables one of a number of code sections to be executed, depending on the value of a <SelectExpression>. On the first line of a CASE block of commands, you specify the variable or expression you want tested.After you have specified the variable or expression, list one or more values or value ranges that the variable can take. There are four ways you can specify cases: Exact Value, Logical Condition, Range, Else.

If <SelectExpression> matches a condition associated with one of the CASE clauses, the statements following that CASE clause are executed up to the next CASE clause, or up to END SELECT for the last one. Control then passes to the statement following the END SELECT. The flow control structure is preferred to IF…THEN when there are many execution paths based on the value.

The <SelectExpression> is compared against as many exact values as necessary, using the CASE <expression> syntax. The <SelectExpression> is logically compared against another expression using the CASE IS <relational-operator> <expression> syntax. Any of the six relational operators may be used (>, >=, <, <=, =, <>). The <SelectExpression> may be compared against a range of values using the CASE <expression> TO <expression> syntax. If you use the TO keyword to indicate a range of values, the smaller value must appear first. If the first expression is greater than the second, the CASE statement is ignored and no error is flagged. If CASE ELSE is used, its associated statements are executed only if the <SelectExpression> does not match any of the other CASE selections.

Syntax

SELECT CASE <SelectExpression>

{CASE <expression>
{statement_list} }
{CASE IS <relational-operator> <expression>
{statement_list} }
{CASE <expression> TO <expression>
{statement_list} }
{CASE <expression> comma <expression>
{statement_list} }
{CASE ELSE
{statement_list} }

END SELECT

Availability

All versions

CASE <expression> comma <expression> added in Version 4.7.1

Scope

Task

Limitations

Do not use SELECT CASE in Config.prg. The SELECT expression is not a relational expression nor a mathematical expression. This means, it is not a condition.

Examples

Program
Dim I as Long
  Select Case I
    Case 0
      Print "I = 0"
    Case 1
      Print "I = 1"
    Case is >= 10
      Print "I >= 10"
    Case is < 0                       'No requirement for statements after Case<0
    Case 5 To 10
      Print "I is between 5 and 10"
    Case 2, 3, 5                      'Added in Version 4.7.1
      Print "I is 2, 3 or 5"
    Case Else
      Print "Any other I value"
  End Select
End Program

See Also