The LabVIEW Equivalent of an "If" Statement
Primary Software: LabVIEW Development Systems>>LabVIEW Full Development System
Primary Software Version: 7.1
Primary Software Fixed Version: N/A
Secondary Software: LabVIEW Development Systems
I am new to programming in LabVIEW, and I am looking for a way in LabVIEW to achieve the functionality of an if, if-else, or switch statement from text-based programming. What is the LabVIEW equivalent of these statements?
The LabVIEW equivalent of the if statement, if-else statement, or the switch statement is the Case Structure from the Structures palette (Functions»Structures»Case Structure). Alternatively for a simple if statement, you also can use the Select Function from the Comparison palette (Functions»Comparison»Select).
The Case Structure defaults to one True case and one False case. For each case, you can write LabVIEW code that will be executed if the condition you set is met. As you can see from Figure 1, the Case Structure has a green question mark input terminal called the case selector terminal. You must wire a Boolean output to this terminal to set the conditions for which case should be executed. The code in Figure 1 is equivalent to an if statement comparing a variable "x" to a constant value of 5. If the variable "x" is equal to 5, the Case Structure will perform whatever code is inside of the True case. If "x" is not equal to 5, the Case Structure will perform whatever is in the False case. In this example from Figure 1, if "x" is equal to 5, a green Boolean front panel LED will receive a value of TRUE because the True Case was selected.
Figure 1 - Case Structure - if statement
Alternatively for simple conditions and actions, the Select Function functions similarly. The Select Function accepts three inputs. Those inputs listed in order from top to bottom can be seen below:
Figure 2 shows equivalent if statement functionality of the Case Structure in Figure 1, but using the Select Function.
Figure 2 - Select Function - if statement
The advantage of using the Case Structure is that you can you can add cases and essentially create if-else statements and switch statements. You can use strings, numbers, or enumerations wired to the case selector input to identify all of the available cases. In LabVIEW 7.1 or earlier, refer to the LabVIEW User Manual (linked below) for detailed information about Case Structures. In LabVIEW 8.0 or later refer to the Case Structure topic in the LabVIEW Help (linked below). There are also several shipping examples for the Case Structure. To access the examples from LabVIEW go to the Help menu and select Find Examples. Then click on the Search tab and enter Case Structure as the search term. A number of examples are available to choose from.
Figure 3 displays an example of a switch statement mentioned above. In this example, the user controls a numeric input. The code accessed and the resulting output of each case is dependent on the numeric input selected by the user.
Figure 3 - Switch Statement - Numeric Input
The architecture of Case Structures used as switch statements offers flexibility in regard to data types the user can input as well as modification for future use. Instead of a numeric input, as was used in the last example, the Case Structure can also accept an easily modifiable enumerated input such as the one in Figure 6. For more information on adding cases to the case structure please see the Adding and Duplicating Subdiagram section of the LabVIEW Help.
Figure 4 - Enumerated Input Values
The accessed portion of code as well as the output, again, is dependent on the input value chosen by the user. This can be seen in Figure 5 below as well as the attached example VI.
Figure 5 - Switch Statement - Case Zero
LabVIEW User Manual
LabVIEW Help: Case Structure
KnowledgeBase 299BTMJ3: How Do I Specify a List or Range of String Values for a Certain Case in a Case Structure?
LabVIEW 2011 Help: Creating an Enumerated Type Control
LabVIEW 2011 help: Adding and Duplicating Subdiagrams
If and Switch Statement Examples.vi
Report Date: 08/31/2000
Last Updated: 10/05/2017
Document ID: 20UCD4PH