# SELECT...CASE

{PBASIC 2.5}

SELECT...CASE Example

Syntax:
SELECT Expression
CASE Condition(s)
Statement(s)
{ CASE Condition(s)
Statement(s)
CASE ELSE
Statement(s) }
ENDSELECT

## Function

Evaluate Expression and then conditionally execute a block of code based on comparison to Condition(s). After executing a block of code the program continues at the line that follows ENDSELECT. If no conditions are found True and a CASE ELSE block is included, the CASE ELSE code statements will execute.

- Expression is a variable, a constant or an expression.
- Condition is a statement, that can be evaluated as True or False. The Condition can be a very simple or very complex relationship, as described below. Multiple conditions within the same CASE can be separated by commas ( , ).
- Statement(s) is any valid PBASIC statement or statements. Multiple statements may be placed on the same line (though not recommended) by separating each statement with a colon ( : ).

## Quick Facts

| BS2 Family
Comparison operators | = (assumed if no operator), <>, >, <, >=, <=
Conditional Logic Operators | NOT, AND, OR, XOR
Condition format | condition_op value or value1 TO value2
Parentheses | Allowed
Nesting | Up to 16 levels
Related Commands | IF…THEN…ELSE, ON...GOSUB, BRANCH

## Explanation

SELECT...CASE is an advanced decision-making structure and is often used to replace compound IF...THEN...ELSE structures. The available comparison operators are:

Comparison Operator Symbol | Definition
= | Equal (assumed)
<> | Not Equal
> | Greater Than
< | Less Than
>= | Greater Than or Equal To
<= | Less Than or Equal To

Example:

      SELECT irCmd
        CASE 0 TO 3
          HIGH irCmd                        ' enable selected output
          FREQOUT Spkr, 50, 880             ' beep

        CASE AllOff, Mute
          OutA = %0000                      ' all outputs off

        CASE ELSE                           ' invalid command
          DEBUG "Bad Command", CR
      ENDSELECT

Here, the BASIC Stamp will examine the value of variable irCmd. If irCmd is between zero and three (inclusive), the associated output (P0 .. P3) will be made high and and the speaker will beep. If irCmd is equal to either AllOff or Mute (constants), then outputs P0 - P3 will be made low. On any other value of irCmd, a message will be displayed that indicates an invalid command.

See IF…THEN for additional details on the use of comparison and conditional operators.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012