LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
BruceAmmons

Equivalent of Switch statement in C

Status: Declined

Any idea that has received less than 2 kudos within 2 years after posting will be automatically declined.

I would like to see a case statement that would work like a switch statement in C.  You could wire a cluster of information to the input that has all the data of interest in it, such as the values of A, B, C, and D.  Each page of the case statement would have a complex logic statement that defined what it handled, such as "A<3 and B>4".  If this was false, it would drop through to the next case.  If none of the cases were true, it would drop down to the default case which would always be last.

 

Another possibility instead of a cluster would be using one input for each variable, just like the formula node uses.  You could enter a name for each input that would be used in the case statements.

 

This would have saved me a bunch of time in a recent program, where there were three possible values for A and three for B, as well as C and D.  The cases ended up being nested four deep, which is something of an editing nightmare.  The ability to do multiple comparisons within a single case statement would have been wonderful.

 

It might be a little confusing to keep track of the order/priority of the cases.  We might need some sort of tool for viewing the whole list of cases and editing each case, sort of like editing the list of values for an enum.

 

This idea was inspired by JeanPierre's request for an if else statement.

 

Bruce

Message Edited by Bruce Ammons on 07-30-2009 10:42 PM
Bruce Ammons
Ammons Engineering
13 Comments
JackDunaway
Trusted Enthusiast

Although this idea could be powerful, I am hesitant to support this idea. Please review this link, and read my comment here, the second from top.

 

The idea proposed by Bruce above is much like lvABC's that I linked to, but I think Bruce's is a bit more elegant. The idea of a "tool for viewing the whole list of cases and editing each case" seems limited enough to prevent chaos, yet it remains powerful; not unlike, say, configuring an Event Structure.

 

Depending on where discussion goes will determine whether I Kudos this idea or not.

BruceAmmons
Trusted Enthusiast

I forgot that the switch statement in C drops through to the next statement after completing the current statement.  The idea I am proposing does not do that - it only executes a single case statement (the first one that evaluates TRUE), just like the current case statement.  There would be no "break" command.

 

I am really just looking for a more powerful case statement that can handle multiple variables at one time.

 

Bruce

Bruce Ammons
Ammons Engineering
Intaris
Proven Zealot
It could be very useful indeed, but it all depends on the implementation.... So much could be done wrong
BruceAmmons
Trusted Enthusiast

Somewhere between going to bed and falling asleep, I realized this is not a switch statement at all.  It really is a series of If/Elseif/Else statements.  I am a little rusty on C.

 

Bruce

Bruce Ammons
Ammons Engineering
jlokanis
Active Participant

Yes.  This is not a switch statement.  LabVIEW already as a primative that is equivalent to the C switch statement, the Select function:

 

select function.jpg 

-John
------------------------
Certified LabVIEW Architect
JackDunaway
Trusted Enthusiast
"Equivalent" is a stretch...
BruceAmmons
Trusted Enthusiast

I would actually say the current case statement is the closest equivalent of the switch statement.

 

Bruce

Bruce Ammons
Ammons Engineering
Intaris
Proven Zealot
The primitive shown by jlokanis is the equivalent of the "conditional Assignment" in C.  See HERE for more info.  It certainly is not an equivalent to the switch statement as it can only ASSIGN values, not execute specific code.
jlokanis
Active Participant

My mistake.  That is the conditional statement.

 

I also agree that the current case statemenet is functionally equivalet to a switch.  You can use almost any variable type as the selector and you can define a default case and assign multiple values of the selector to a single case.  That seems to satisfy the description on Wikipedia...

-John
------------------------
Certified LabVIEW Architect
Intaris
Proven Zealot

A switch statement is called "case" in Pascal.