PDA

View Full Version : Broken Case statement Logic



Tom Gonser
- 8th June 2005, 06:33
I have a program that assigns ASCII values based on a number. In a particular case, the value is '27', I know because I have it Serout showing me this.

BUT, the Case statement is not catching it.. It should be falling into the "do a different thing" bucket, but it does not..

In this example, TC1 = 27:

Select Case TC1
case 0
TracID1 = " "

case TC1 > 0 and TC1 < 27
Do One thing

case TC1 >= 27 and TC1 < 37
Do a different thing
case 37
TracID1 = ":"
case 38
TracID1 = "."
case else
TracID1 = "$"
End Select


Since the value being compared is in fact "27" why does it fall all the way down to ELSE, and assign it a value of "$"???

What's wrong with the logic?

Tom

Dave
- 8th June 2005, 12:05
Tom Gonser, I beleive the variable to be used in the case statement is to only be declared in the first line of the Select Case. Also the optional directive "IS" is to be used for other than compare functions. I have never seen code written the way you have expressed it in your question. I would have done it this way:

Select Case TC1

case 0 'actually equals "0"
TracID1 = " "

case is < 27 'catches all instances between "1" and "26"
Do One thing

case is <37 'catches all instances between "27" and "36"
Do a different thing

case 37
TracID1 = ":"

case 38
TracID1 = "."

case else
TracID1 = "$"

End Select

You would place the statements in an order as to catch them in order.

Dave Purola,
N8NTA

Tom Gonser
- 8th June 2005, 15:18
How odd.. I actually had it working last night, but this AM tried the new suggestion and it broke again. Unfortunately I was an idiot and did not write down what worked. I THINK it was:

Select Case TC1
case 0
TracID1 = " "

case TC1 < 27 'A-Z
TracID1 = TC1 + 64

case TC1 >=27 & TC1 < 37 '0-9
TracID1 = TC1 + 21

case 37
TracID1 = ":"
case 38
TracID1 = "."
case else
TracID1 = "$"
End Select

But it does not work again. As a matter of fact, I tried just testing with a fla out test -

case TC1 = 27

and with a value of '27' showing up on my hyperterminal screen, the case logic is not catching it as '27'.... I really don't get that, it is almost like the compiler is not building this code right.

TG

Tom Gonser
- 8th June 2005, 15:34
... actually if I have PBP show me the 'DIG1' of the value that was 27, it is actually 27.7... it needs to be just 27.. How does one round using PBP?

Dave
- 8th June 2005, 16:24
Tom, Where is the value for the variable TC1 comming from?

Dave Purola,
N8NTA

Tom Gonser
- 8th June 2005, 17:38
It is coming from an array of 2 HEX bytes, which are then converted to binary. In the case I am using, the

the value '44308' is what TRACID_ID is in this example..

SSD4= (ssmax[1]>>4)
SSD3= (ssmax[1] & $f)
SSD2= (ssmax[0]>>4)
SSD1= (ssmax[0] & $f)

TracID_D=(ssd4*4096)+(ssd3*256)+(ssd2*16)+ssd1' convert to dec

TC1=(TracID_D/1600)

I have attached a spreadsheet that does this also. It uses an "INT" function to keep the result of the TracID_D/1600 rounded to 27. In PBP I need an equivalent function. Otherwise the answer is 27.7 and that messes up the CASE statement.

IN the excel example, if you remove the INT function, it gives 27.7 also.

TG

Tom Gonser
- 8th June 2005, 21:13
WOW!! I just tried setting my value to 27 manually, and it STILL does not work!!

Check this out:


TC1=27


Select Case TC1
case 0
TracID1 = " "
case TC1 < 27 'A-Z
TracID1 = TC1 + 64
case TC1 >= 27 '0-9
TracID1 = TC1 + 21
case 37
TracID1 = ":"
case 38
TracID1 = "."
case else
TracID1 = "$"
End Select

The result is "[", when it should be "0"!!! How could I be setting a value of "27" and having this kind of result?

TG

Dave
- 8th June 2005, 21:44
Tom, If you would please try the code exactly as I have written it in my first reply and tell me what the results are.

Dave Purola,
N8NTA

Tom Gonser
- 8th June 2005, 21:54
The answer was to not use the CASE statement at all.. It looked like it was failing somehow internally. So I moved to:

If TCX = 0 then TracID1 = " "
If TCX < 27 then TracID1 = TCX+64
If TCX >=27 then TracID1 = TCX+21
If TCX = 37 then TracID1 = ":"
If TCX = 38 then TracID1 = "."
if TCX > 38 then TracID1 = "$"

I think the code is smaller, and best of all it works.

Dave
- 8th June 2005, 21:58
Tom, It only goes to show, There's more than 1 way to skin a cat.......

Dave Purola,
N8NTA