LabVIEW Idea Exchange

About LabVIEW Idea Exchange

Have a LabVIEW Idea?

  1. Browse by label or search in the LabVIEW Idea Exchange to see if your idea has previously been submitted. If your idea exists be sure to vote for the idea by giving it kudos to indicate your approval!
  2. If your idea has not been submitted click Post New Idea to submit a product idea to the LabVIEW Idea Exchange. Be sure to submit a separate post for each idea.
  3. Watch as the community gives your idea kudos and adds their input.
  4. As NI R&D considers the idea, they will change the idea status.
  5. Give kudos to other ideas that you would like to see in a future version of LabVIEW!
Top Kudoed Authors
cancel
Showing results for 
Search instead for 
Did you mean: 

Equivalent of Switch statement in C

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
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.

Wirebird Labs: Expert Toolkits for LabVIEWDeploy, by Wirebird Labs: Expert Toolkits for LabVIEW
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
Trusted Enthusiast
It could be very useful indeed, but it all depends on the implementation.... So much could be done wrong
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
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
Trusted Enthusiast
"Equivalent" is a stretch...
Wirebird Labs: Expert Toolkits for LabVIEWDeploy, by Wirebird Labs: Expert Toolkits for LabVIEW
Trusted Enthusiast

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

 

Bruce

Bruce Ammons
Ammons Engineering
Trusted Enthusiast
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.
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
Trusted Enthusiast

A switch statement is called "case" in Pascal.