PDA

View Full Version : SELECT CASE for ranges of values



caverman
- 1st April 2006, 14:39
Sure this may be basic stuff (pun intended) but can you have a range of values for a select case

Eg Visual basic allows
SELECT CASE PulseLength
CASE 165 TO 176
blah
CASE blah...

I want to do something like
select case PulseLength
case 165 to 176
OutValue=0
case 184 to 192
OutValue=1
end select

But it don't work

Any ideas?
Thanks
Dirk

caverman
- 1st April 2006, 14:42
or something like
select case PulseLength
case is > 165 and is < 176
OutValue=0
case is > 184 and is < 192
OutValue=1
end select

Dirk

Archilochus
- 1st April 2006, 16:11
Seems like maybe some 'IF... THEN's might work out better?

caverman
- 2nd April 2006, 00:57
Funny how late at night one gets stuck into a Select method without thinking of the obvious easy If solution

caverman
- 2nd April 2006, 00:58
But maybe the PICBASIC people could add the CASE x TO y variety that some BASICs have.

Dave
- 2nd April 2006, 13:02
caverman, Why make a mountain out of a Mole hill. Just do this:

select case PulseLength
case is < 165
you don't want these values because they are below 165 so do something
case is < 176
OutValue=0
case is < 184
you don't want these values because they are between 176 and 184 so do something
case is < 192
OutValue=1
case else
you don't want these values because they are between 192 and 255 so do something
end select

This is the correct way as far as I can see to do this type of compare situation. If you were to use "IF" statements and try to capture all posibilities the code would look much different.

Dave Purola,
N8NTA

Archilochus
- 2nd April 2006, 15:20
That looks like a nice clean solution Dave!

sayzer
- 3rd April 2006, 06:51
Why not using the statement below?



IF PulseLength > 165 AND PulseLength < 176 THEN
OutValue=0
ELSE
IF PulseLength > 184 OR PulseLength < 192 THEN OutValue=1
ENDIF


----------------------

Ioannis
- 3rd April 2006, 08:56
Why not using the statement below?

IF PulseLength > 165 AND PulseLength < 176 THEN
OutValue=0
ELSE
IF PulseLength > 184 OR PulseLength < 192 THEN OutValue=1
ENDIF

----------------------

Because the above compiles almost 2.5 times longer!

Ioannis

sayzer
- 3rd April 2006, 16:14
If you meant the size in terms of the words used,

I had 103 words with "IF" statement and 110 words with "Select Case" statement.

How did you have 2.5 times more words?

Dave
- 3rd April 2006, 17:28
sayzer, You are right, it maybe smaller but you have only captured half of the cases. What are you going to do about the rest of the cases ie: < 165, > 176 but < 184, and > 192?

Dave Purola,
N8NTA

picster
- 4th April 2006, 01:39
Would you have to jump out to a common goto though, after each < completes its routine? otherwise it'll execute all the ones that apply (i.e. - if your number is 130, and you have a case <140, case <150, case <160, case <170, etc. ALL apply after case <130), or is the nature of the select case such that the first one that applies ONLY is executed?

--------------Picster----------------

Ioannis
- 4th April 2006, 07:00
If you meant the size in terms of the words used,

I had 103 words with "IF" statement and 110 words with "Select Case" statement.

How did you have 2.5 times more words?

Hi!

With the F877 chip I got 43 and 114 for the two codes.

What chip did you try?

Generally you have to keep the if statements as "thin" as possible. An AND can be replaced by one more IF resulting in less words.

Ioannis

Dave
- 4th April 2006, 13:35
picster, Yes you are correct. The first true instance in the case statement flow is the final test before the end select .

Dave Purola,
N8NTA

sayzer
- 5th April 2006, 06:21
Hi Ioannis,

I compiled the code with F628A.

Pls check this post at http://www.picbasic.co.uk/forum/showthread.php?t=3548

From PIC to PIC "words used" changes as you may guess.


-------------

Ioannis
- 5th April 2006, 08:51
Hi Ioannis,

I compiled the code with F628A.

Pls check this post at http://www.picbasic.co.uk/forum/showthread.php?t=3548

From PIC to PIC "words used" changes as you may guess.
-------------

Yes it changes indeed. But it is a little bizzare to have such differences for devices so similar (628-877).

I put only the modefs include. Did you put any other? Other defines?

Ioannis

sayzer
- 6th April 2006, 15:02
I did not include any modifier file.
Just the code itself.

Meanwhile, lets take your "select case" and compile it with
16F628A
16F877
12C509A

The result is:

16F628A = 95 words
16F8777 = 110 words
12C509A = 123 words

How interesting!

Ioannis
- 6th April 2006, 15:23
...
The result is:

16F628A = 95 words
16F8777 = 110 words
12C509A = 123 words

How interesting!

Especially for the smallest part that has the largest output!!!

Ioannis