LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to write a fall-through switch statement?

I am trying to write the equivalent of a C-style
switch statement. The attached VI has 2 ways of
accomplishing this. Is there a better way? What are
the pros/cons of the two ways shown in the VI?
0 Kudos
Message 1 of 5
(2,753 Views)
I'm not sure that I've understood you correctly but I think that Case structure is equivalent of switch in C.

But I have written the example which does the same thing as your VI without any cases. Look at this it may be interesting.

Good luck.

Oleg Chutko
0 Kudos
Message 2 of 5
(2,753 Views)
That does mimic my string example, but it doesn't solve the general situation. The switch in C is exactly like LabVIEW's Case Structure, but with one interesting property: you can set it up so that whichever case executes first, all cases after that will also execute. This is frequently useful when you have several values that need various amounts of work done and those work quantities are subsets of each other. The LV case structure only executes one frame of the structure. It would be pretty tricky, given the nature of dataflow, for NI to build a structure that can "fall through" the way C's switch can. That's why I was designing groups of structures that have this behavior. The two I showed in my original post are the only ones I have been able to com
e up with thus far.
0 Kudos
Message 3 of 5
(2,753 Views)
I have no better way of doing this, but I'll give you my opinion as to the
readability and the maintainability.

Readability:
I find option 2 more readable, the entire code is laid out and easy to
see without cycling through cases. It is pretty easy to see what it does
and how it works. Option 1 requires a little more thinking to follow the
logic, but is not undecipherable.

Maintainability:
I would think that option 1 is much more maintainable. Simply add
another case to the case structure and you're ready to go. No rewiring, no
need to clear out more room on the diagram. Seems easy to me. Option 2 is
going to require a little more work, and if room is already scarce on the
diagram, adding another case structure may make wiring more
difficult.

Overall:
I'm not sure I see where I would incorporate this in my code right now,
but if the need arose, I would probably do something like Option 1.

My thoughts

Spencer Anderson

"Jabberwocky" wrote in message
news:506500000005000000BC5D0000-1012609683000@exchange.ni.com...
> That does mimic my string example, but it doesn't solve the general
> situation. The switch in C is exactly like LabVIEW's Case Structure,
> but with one interesting property: you can set it up so that whichever
> case executes first, all cases after that will also execute. This is
> frequently useful when you have several values that need various
> amounts of work done and those work quantities are subsets of each
> other. The LV case structure only executes one frame of the structure.
> It would be pretty tricky, given the nature of dataflow, for NI to
> build a structure that can "fall through" the way C's switch can.
> That's why I was designing groups of structures that have th
is
> behavior. The two I showed in my original post are the only ones I
> have been able to come up with thus far.
0 Kudos
Message 4 of 5
(2,753 Views)
Hi,
I've got my mistake.
I think that the state machine solution is the best. It takes less place on the diagram and it looks more elegant.
Good luck.

Oleg Chutko
0 Kudos
Message 5 of 5
(2,753 Views)