LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Best equivalent of an if else statement in labVIEW?

Solved!
Go to solution

I'm trying to figure out the best way to use one value to control what value is selected as an output in labVIEW.

 

Say I have the following code:

 

If (x <= 500){

    output = 16;}

elseif( x > 500 && x <= 1000){

    output = 8;}

else{

    output = 4;

}

 

My original thought was just to use a case structure, but it seems like you can't enter numeric expressions into the different cases. Is there a simple way to do this in labVIEW that I have overlooked, or does it just require using a few different select and comparison functions?

0 Kudos
Message 1 of 17
(8,988 Views)
Solution
Accepted by topic author randomguy77

You can put ranges into the case structure.  In your example you can use the following cases:

..500

501..1000

Default

 

Notice those ".."?  That indicates the range.  So the first case will be anything less than or equal to 500.  The second will be from 501 to 1000.  You can also use commas to create a list of items.


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 2 of 17
(8,973 Views)

@randomguy77 wrote:

My original thought was just to use a case structure, but it seems like you can't enter numeric expressions into the different cases. Is there a simple way to do this in labVIEW that I have overlooked, or does it just require using a few different select and comparison functions?


Sure you can.  You can enter ranges of values (e.g. 500..1000) or single values.  Check the help file for the case structure for more details.

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 3 of 17
(8,972 Views)

Remember that the simplest form of a case structure is a True/False, and that is exactly what your text-based code is based on.  You can do comparisons on the number and drive a selector of the case structure with that.  Elses in text are the equivalent of the False case.  And ElseIF is the equivalent of doing another comparison in the False case and driving another case structure inside of it.

 

I don't like the idea of nesting case structures too deeply, but the literal translation of your code would have a case structure inside the false case of a larger one.

0 Kudos
Message 4 of 17
(8,960 Views)

One caveat on using a Case Statement with individual or ranges of numbers as the Case Selectors -- this only works for things that resolve to integers, i.e. it will not work for Floats (because it depends on comparisons which are very tricky with "approximations to real numbers").

 

Bob Schor

0 Kudos
Message 5 of 17
(8,952 Views)

A couple of quick thoughts based on this thread:

 

Don't use embedded case structures here.  While it does match the literal translation of your code, I'd push you towards using two Select functions instead.  Since you're not doing any logic, the Selects work well with constants.  It makes the code more readable and gets away from the embedded structures.  Generally, if I'm using a true/false and constants, I'll skip the case structure.

 

For case structures and floats, you can get around that limitation by multiplying the floating point value by 10^(decimal places).  For example, multiplying by 100 means you'll get to check to two decimal places.  You'll just have to match that multiplication in the case settings for each case.

Message 6 of 17
(8,912 Views)

As a similar note to this thread, how would I achieve this:

 

if(n>x){

do something.

}

elseif(v>y){

do something.

}

elseif(v<z){

do something.

}

else{

do something.

}

 

Note that the compared variables are different. I'm not sure I see an obvious way to do this that doesn't require a bunch of cluttered select functions, though that might be the way.

 

EDIT: Also, the function should "break" on the first condition that evaluates to true.

0 Kudos
Message 7 of 17
(8,486 Views)

Assuming n,x,y,v,z are all available from the beginning, do all the comparisons, build the results into  a Boolean array in the desired precedence,  search for the first true an wire the resulting index to a case structure with as many cases you need. Case "-1" contains the code of the last else.

Message 8 of 17
(8,483 Views)

Thanks for taking the time! That's certainly more elegant than what I would have ended up with in LabVIEW, and what I'll do now. 

 

Am I approaching this wrong? Trying to shoehorn text-based programming into LabVIEW? Is there a way I can approach this problem that lends itself more to graphical programming?

 

Edit: Nevermind, that came out sexy.

0 Kudos
Message 9 of 17
(8,470 Views)

@Disappointed_ wrote:

 

Is there a way I can approach this problem that lends itself more to graphical programming?


Yeah, forget everything you know about text based programming. Smiley Happy

 

Okay that isn't 100% true.  But there is some truth to the fact that texted based programmers come in with some concepts that don't always translate well.  I have a friend of mine that believes he can master any programming language in a couple weeks.  He believes all he needs to do is understand the new syntax since programming concepts and the things he learned translates to these other languages.  For the most part I understand that and I too try taking concepts I learned in Java, and apply them to C++.  But when it comes to G I think less things carry over, and the best way to understand LabVIEW is to have a good mentor, try things and fail, and take training, in that order.

Message 10 of 17
(8,454 Views)