Technical Article

Decision-making Structures in LabVIEW

September 02, 2021 by Seth Price

Choosing between a select? structure and a case structure to form your decision-making structure in LabVIEW? In this article, we’ll review how each of these structures suits different applications.

A program that does not make any decisions is limited in the real world. Automation places as many decisions on the computer as possible. Because machines can quickly make simple decisions, they can control movement, enhance safety, and do other tasks that do not require creative input from a human.

In text-based languages, decisions are performed through “if-then-else” statements or through “switch” or “case” statements. LabVIEW does not have “if-then-else” statements, but instead uses specific structures for decision-making capabilities. In particular, LabVIEW uses “select?” structures and “case” structures for most of its decision-making.

These decision-making structures are programming pieces, so they belong in the block diagram of a LabVIEW virtual instrument (VI). To access either of these structures, the programmer must look in the functions palette.

 

Select? Structure

A “select?” structure has three total inputs and one output. The center input is a boolean (true/false) type. If the center input is “true,” the top input is transferred to the output. If the center input is “false,” the bottom input is transferred to the output. Another way to look at it is the “T” input is what will be outputted in a true input, the “F” input is what will be outputted with a false input, and the “?” is the boolean input that governs the decision.

 

Figure 1. Select? structure.

 

To place a select? structure, open the functions palette, then the comparison menu. The select? structure is simply dropped on the block diagram.

 

Case Structures

Case structures allow the programmer to choose between multiple outputs. The input can be any boolean or enumerated data type, such as integers or enums. If the input is a boolean, the only two cases will be “true” and “false.” But if an integer or enum is used, the case structure can handle many cases.

Using an enum allows the programmer to create types that help document the code, such as days of the week, months, procedures, steps, or another label. The labels are for human use; LabVIEW recognizes the enum as an integer.

The input data type can be a string or a double-precision floating-point, though they have to match the case exactly. For example, perhaps the input is the string “Wednesday,” but if an operator types “wednesday,” the case will not match, and the VI will choose the default case.

The case structure can be found in the functions palette under the structures menu. It is placed just like loops are in LabVIEW by clicking the location of the top left-hand and dragging it to size. LabVIEW treats the case structure like a stacked set of block diagrams, such that the programmer can toggle between each case and add code.

The bar along the top can toggle between cases or select a case to code. The “?” terminal is where the selector criteria input is connected. Notice that once this is connected, the case structure populates with a few cases. To add all possible cases, right-click on the edge of the case statement and select “add case for every value.”

 

Figure 2. This case has been fed an enum with a list of days of the week. The case structure is auto-populated so that each day has its own case.

 

Notice that one case is listed as the “default” case. The default case is the case that gets selected if no inputted value matches any of the cases. Every case structure requires a default case, and the programmer can pick one.

The LabVIEW VI will not run if there is no default case. The VI also must have an output for each output terminal in every case. If a case is missing an output, the terminal will be hollow, and the code will not run.

 

Figure 3. The bottom output terminal is missing an output in one case. Because this is the “true” case, and an output is attached, the “false” case is likely unwired.

 

Deciding Which Decision-making Structure to Use

While each of these decision-making structures can work for any situation, the programmer must ask, “When should I use one over the other?”

Select? structures take one boolean input signal and use it to pick one single output. Therefore, any time the programmer needs to control the output of one signal based on one single input, the select? structure is the best choice. It has less programming overhead and executes much faster than the case structure.

For example, a certain VI needs to send a simple status string that says “paused” or “running” to a display. A boolean input can determine this, and the output is simply one string or the other.

 

Figure 4. Select? structure deciding between two different strings.

 

Case structures are a better fit for the code if the VI must decide between more than two options, or if more than one output is required. Instead of using only a boolean input, a case statement can decide between multiple options, such as choosing between integers or enum numeric types.

For example, maybe different automatic cleaning routines for a chemical reactor are scheduled for different days of the week. An enum data type can be created with days of the week as its inputs, such that a numeric “0” means “Sunday,” a “1” means “Monday,” and so on. The VI can get the day, then use that as input to a case structure. The case statement will navigate to the proper code, and the VI will implement the correct cleaning routine.

Another example in favor of case structures would be when a routine has to do multiple things based on one decision. Suppose a VI must send control signals to multiple valves based on feedback from a flow meter. A comparison is made between the flow rate and a set point, and a boolean is fed into the case structure. The case sets the proper valve position for every valve, each of which will have its own output from the case structure. This would be difficult to do with select? structures.

 

Figure 5. True/false case statements.

 

A case statement outputs different values to five valves, depending on the flow rate status (flow high?). They are the same piece of code, but displayed twice to show the difference between the true and false cases.

The programmer should also build for future expansions. Maybe a decision will only have a boolean input and one output today, but there may need to be more input options or outputs in the future. If this is the case, the programmer should choose the case structure.

For quick coding, a select? structure allows the VI to choose between two options, just like an if-then statement in a text-based language. For more options, a case structure is a powerful tool that can control program flow, assign multiple outputs, and be used in several types of common LabVIEW architectures.