SELECT...CASE

BS2 icon BS2e icon BS2sx icon BS2p icon BS2pe icon BS2px icon {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.

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 PBASIC Language home page

Open Getting Started with Stamps in Class

Open Connection Troubleshooting

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/21/2013