Shop OBEX P1 Docs P2 Docs Learn Events
using IF THEN with ranges? — Parallax Forums

using IF THEN with ranges?

TonyATonyA Posts: 226
edited 2005-09-09 21:35 in BASIC Stamp
Hi,

I was using this:

IF result = 2 TO 10 THEN
GOSUB sub_routine

How can you use a range like this?

or must it be a string of code like:

IF result <= 1 THEN
GOSUB sub_routine
ENDIF

IF result >= 2 THEN
GOSUB sub_routine
ENDIF

Thank you,

TonyA

Post Edited (TonyA) : 9/9/2005 7:47:55 PM GMT

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-09-09 20:06
    Tony,

    ·· How about:

    IF result < 2 or result > 10 THEN
       GOSUB sub_routine
    ENDIF
    


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-09-09 20:08
    Tony -

    Generally, when working with ranges of data, the SELECT - CASE statement is a good choice, and is nearly self-documenting, if done right. Check the online PBASIC HELP file for details.

    Regards,

    Bruce Bates
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-09 21:06
    If testing just one range, use IF-THEN as it's more efficient.· For your range of 2 to 10 (inclusive), you'd do this:

    · IF ((result >= 2) AND (result <= 10)) THEN
    ··· ' do something
    · ENDIF

    Note the use of parenthesis -- while not required, the use of parenthesis helps the code look neater and easier to read.· If you need to do separate things for different ranges, use SELECT-CASE like this:

    · SELECT result
    ··· CASE 2 TO 10
    ····· ' do something
    ··· CASE 15 TO 18
    ····· ' do something else
    ··· CASE > 25
    ····· ' do something else altogether
    · ENDSELECT

    These programming things are covered in What's a Microcontroller? -- that book may be worth a read (and you can download it for free!).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 9/9/2005 9:47:44 PM GMT
  • TonyATonyA Posts: 226
    edited 2005-09-09 21:35
    Hi,

    Yes, this is great. I'll be using the Select Case thing since I have a number of ranges I need to work with.

    Thanks for the info and help.

    TonyA
Sign In or Register to comment.