Shop OBEX P1 Docs P2 Docs Learn Events
Syntax / sanity check — Parallax Forums

Syntax / sanity check

KenMKenM Posts: 657
edited 2005-05-10 14:34 in BASIC Stamp
BS2p...

Using IF, I can use a compound AND condition·such as;

IF  gear >90 THEN FIRST
IF  (gear > 50) AND (gear < 91) THEN SECOND
IF  (gear > 35) AND (gear < 51) THEN THIRD
IF  (gear > 14) AND (gear < 36) THEN FOURTH

But can the same be done with Select Case? (I get error messages with various iterations using AND, or colon etc)

 SELECT gear
   CASE > 90                
       'some code
 
   CASE (> 50 ) AND (< 91 )
       'some other code
 
   'etc, etc
 
 ENDSELECT 


 SELECT gear
   CASE > 90                
       'some code
 
   CASE (gear > 50 ) AND ( gear < 91 )
       'some other code
 
   'etc, etc
 
 ENDSELECT 

From the help file (yes I read it)...

···SELECT Expression
· · ·CASE Condition(s)

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 ( : ).

Bottom line...Can an AND condition be used with CASE, and if so, the proper syntax is?

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-05-10 12:27
    Yes.· You would use "TO" to express a range of values as shown below:

    · SELECT gear
    ·· ·CASE·> 90
    ··· · ' first
    ··· CASE 51 TO 90
    ···· ·' second
    ·· ·CASE 36 TO 50
    ···· ·' third
    · · CASE 15 TO 35
    ··· · ' fourth
    · · CASE ELSE
    ··· · ' bad value
    · ENDSELECT

    Another way to do the range thing -- that more closely matches your IF-THEN construct -- is like this:

    · SELECT gear
    ·· ·CASE·> 90
    ··· · ' first
    ··· CASE·> 50, < 91
    ······' second
    ·· ·CASE > 35, < 51
    ······' third
    · · CASE > 14, < 36
    ····· ' fourth
    · · CASE ELSE
    ··· · ' bad value
    · ENDSELECT

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • KenMKenM Posts: 657
    edited 2005-05-10 13:37
    Thank you Jon

    ·P.S. Did I miss the solution in the help file? I thought I read it carefully.

    I am using V2.2

    Post Edited (KenM) : 5/10/2005 1:43:35 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-05-10 14:19
    Ken,

    ·· Yep, it was there...tongue.gif
    Help File said...
    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 ( , ).


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • KenMKenM Posts: 657
    edited 2005-05-10 14:34
    I hate it when that happens !
Sign In or Register to comment.