LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

multiple if...else - is case the only solution?

Solved!
Go to solution

Hi guys,

I need to create a VI with if-else statements. The problem is that there is 5 such cases. Is there any better solution than using nested cases structure?

To be specific, I want to set a certain wait time for a timer for different input frequencies, ie:

if frequency <500 then time = 3000 ms

if 500<= frequency < 1000 then time = 2500 ms

if 1000 <= frequency <5000 then time = 3000 ms

and so on, I will get probably up to ten such ranges.

I would appreciate any idea! 10 nested cases would look rather silly...

0 Kudos
Message 1 of 10
(5,451 Views)
Solution
Accepted by topic author MsEngineer

you can wire the numeric value directly to the case selector and set a number of ranges (don't forget to set a default case)

http://zone.ni.com/reference/en-XX/help/371361K-01/glang/case_structure/

http://zone.ni.com/reference/en-XX/help/371361K-01/lvhowto/case_selector_values/

__________________
Engage! using LV2015
0 Kudos
Message 2 of 10
(5,446 Views)
Solution
Accepted by topic author MsEngineer

The case structure can accept booleans (and error clusters), numbers (integers), strings, and enums to its case selector tunnel.  In your case, a number.  And you can have ranges too.  So your cases will be "..499", "500..1000", etc.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 10
(5,406 Views)

I've done something like this if I expected the values to change:

snippet.png

(As shown, this makes the case statement also able to process floating point numbers, but is a little less self-documenting.)

 

You can also use Boolean Array to Number instead of the 1D Search Array, but I feel my method is a bit easier to understand.  I will often use code that seems more straightforward but less optimized to more clearly show what is happening if I feel the performance hit is negligible.  It is my thought process that creating developer-friendly code helps make it easy to maintain.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 4 of 10
(5,393 Views)
Solution
Accepted by topic author MsEngineer

@billko wrote:

snippet.png

 

You can also use Boolean Array to Number instead of the 1D Search Array, but I feel my method is a bit easier to understand.  


If you do use the boolean array to number, changing the radix view on the case structure to be binary helps sort out what case will be called.  But I agree that it still isn't very readable. 

Message 5 of 10
(5,349 Views)

Thanks, it works and looks good!

0 Kudos
Message 6 of 10
(5,336 Views)

Personally, I would just threshold into an array of frequencies and use the correctly rounded output to index into an array of times. No case structure needed. 🙂

Message 7 of 10
(5,317 Views)

@altenbach wrote:

Personally, I would just threshold into an array of frequencies and use the correctly rounded output to index into an array of times. No case structure needed. 🙂


That's because you don't play fair.  😉

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 8 of 10
(5,307 Views)

@billko wrote:

@altenbach wrote:

Personally, I would just threshold into an array of frequencies and use the correctly rounded output to index into an array of times. No case structure needed. 🙂


That's because you don't play fair.  😉


Fair enough. 😄

 

Here's how it could look like. Easily expandable by just adding elements to the array constants. No code changes needed and everything visible at all times. (adding cases to a case structure is much more work, hides more and more code, and is also error prone!). Note that the array of frequencies needs to be sorted, of course.

 

The code might need to be tweaked depending if you want to include the boundaries into the lower or upper range, so modify as needed. Of course boundaries in DBL are not necessarily what you think they are, so be careful)

 

Message 9 of 10
(5,283 Views)

@altenbach wrote:

@billko wrote:

@altenbach wrote:

Personally, I would just threshold into an array of frequencies and use the correctly rounded output to index into an array of times. No case structure needed. 🙂


That's because you don't play fair.  😉


Fair enough. 😄

 

Here's how it could look like. Easily expandable by just adding elements to the array constants. No code changes needed and everything visible at all times. (adding cases to a case structure is much more work, hides more and more code, and is also error prone!). Note that the array of frequencies needs to be sorted, of course.

 

The code might need to be tweaked depending if you want to include the boundaries into the lower or upper range, so modify as needed. Of course boundaries in DBL are not necessarily what you think they are, so be careful)

 


I am ALWAYS learning stuff from you.  Thank you!

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 10 of 10
(5,192 Views)