News:

Let's find out together what makes a PIC Tick!

Main Menu

Looking for alternative methods to test 2 values

Started by TimB, Jun 19, 2023, 02:24 PM

Previous topic - Next topic

TimB


Hi all

I have some code

If wPostOrificePress < cPressZMax or wPostOrificePress > cPressSMin Then
xxxxx
Endif

Works fine
But now I want the opposite without screwing up the logic

What I'm currently doing is

If wPostOrificePress < cPressZMax or wPostOrificePress > cPressSMin Then

Else
     xxxxxxx
Endif
           

Is there a better way?

Thanks

Tim
           

trastikata

Hi TimB,

If you are checking whether wPostOrificePress is between cPressSMin and cPressZMax (I am guessing by the names) then the "OR" will screw the logic in the expression - should be "AND" ... unless cPressSMin is not necessary always smaller than cPressZMax.

QuoteIs there a better way?

The former expression and variables' names makes it a bit confusing - could you narrate the exact logic that you would like to test for?

Fanie

Looks fine to me.

If wPostOrificePress < cPressZMax then

If wPostOrificePress > cPressSMin Then

TimB

Quote from: trastikata on Jun 19, 2023, 04:55 PMHi TimB,

If you are checking whether wPostOrificePress is between cPressSMin and cPressZMax (I am guessing by the names) then the "OR" will screw the logic in the expression - should be "AND" ... unless cPressSMin is not necessary always smaller than cPressZMax.

QuoteIs there a better way?

The former expression and variables' names makes it a bit confusing - could you narrate the exact logic that you would like to test for?

The routine is checking that the value is in the range 0-cPressZMax  or it is >cPressSMin
Where cPressZMax  = pressureZeroMax = 100 and cPressSMin = PressureSpanMin = 2000

I need to test if in the range 100 - 2000

So I could write
If wPostOrificePress > cPressZMax and wPostOrificePress < cPressSMin Then

I had not thought of the "and"  ::)

top204

#4
You can also use Select-Case-EndSelect for comparing if a value is within a certain range:

    Dim wPostOrificePress  As Word
    Symbol cPressSMin = 2000
    Symbol cPressZMax = 100
   
    Select wPostOrificePress
        Case cPressZMax To cPressSMin
            ' Perform the code here because wPostOrificePress is within range
    EndSelect

It also produces tighter code as it does not need to use the expression parser stack, because there are no possible extra operators within a comparison. It is a simple comparison of a single variable.

TimB


Thanks Les

Great advice one to remember


John Lawton

Yes, I love the SELECT.. CASE statement, it is powerful and very readable.