using IF THEN with ranges?
TonyA
Posts: 226
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
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
·· How about:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
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
· 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
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