LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

difference between select and case?

Hello,

 

A question came to my mind: what are the main differences between using a "case structure" and a "select" comparison? I usually use select when I want to see the whole process in front of my eye while I use a case structure when I am lacking space or need to embed multiple case (like to do a "if elseif" type of operation). But is there a "proper use" of when to use one or the other?

 

 

Cheers,

T

0 Kudos
Message 1 of 8
(4,317 Views)

I would make this argument:

 

A Select can always be replaced by a case structure to get the same output.

The converse is not true.

 

So, if Select is still a valuable component of the language it must offer value the case structure does not.

 

I use select over case wherever is convienient and rarely use a boolean case (Error in is a notable exception)


"Should be" isn't "Is" -Jay
0 Kudos
Message 2 of 8
(4,313 Views)

Select can only pick between exactly two inputs.

 

Case structures can have many cases and each case can have lots of code that should execute as a function of the condition. Cases can even be empty if nothing should happen. Case structure can have zero to many inputs and zero to many outputs.

0 Kudos
Message 3 of 8
(4,311 Views)

Well a case is much more flexible, but they both have a purpose.

 

In a text based language you have a Switch operation, which can do operations and so can an IF.  They both server a purpose and I wouldn't recommend getting rid of one in favor of the other.

 

Similar with Case and Switch.  Sure a Case structure has more functionality, but in a punch when I'm selecting between one of two values, using a boolean, a switch is simpler and doesn't take up much space.

0 Kudos
Message 4 of 8
(4,306 Views)

Case structures offer short-circuiting, only code which is necessary is run.  

 

Select requires all code to run regardless of which is chosen.  Select is perfectly happy choosing between a huge array and that huge array incremented by 1, but that huge array must still be copied and incremented whether or not it is going to be chosen.  Many times it is fine, you have values that you calculated anyway or constants that you are choosing from.

 

So, when do I suggest you use Select?  Never!  Use Swap, it is faster and more flexible.  You do not have to swap wires or invert the logic, simply choose the top or bottom output (or sometimes even both).

Message 5 of 8
(4,294 Views)

@ho^3vah^2 (sorry had to =p)

That's the way I see it too. Case is more powerful while select is a simple solution for a two cases choice. Was wondering if there was more than that in "best practice", but it seems that's the way 🙂

 

 

@Darin.K

Good to know! Thanks

0 Kudos
Message 6 of 8
(4,290 Views)

I generally use Select when choosing between two values that are already available, and a case structure when some computation is necessary. All inputs must be available before Select can execute, so if you have to do two computationally-intensive operations, the code will calculate both and then discard the results of one. If you instead use a Case structure, only the code that computes the result that will eventually be used will execute. Don't take this to extremes, though - I have Selects all over my code where I'm choosing between a value, and that same value incremented or decremented. The tiny amount of memory and execution time saved in that situation isn't worth the block diagram clutter that a Case would introduce.

Message 7 of 8
(4,287 Views)

Or, in other words, It certainly is not a one-size-fits-all decision.  weigh the cost of the situation.  then see if even other solutions might benchmark better for that use.


"Should be" isn't "Is" -Jay
0 Kudos
Message 8 of 8
(4,249 Views)