Introduction
This lesson shall introduce the use of Case Structures and Sequence Structures in LabVIEW. In LabVIEW, Case and Sequence Structures allow the developer to control the course of execution of a VI based on information provided by a user or by some pre-calculated piece of data.
The case structure is somewhat analogous to the IF-THEN-ELSE statement common in traditional text-based programming languages allowing developers to create conditional architectures that control which sections of code are executed. The Sequence Structure adds control to the order of execution of the VI, which is particularly important when working in a dataflow based programming language – remember that in LabVIEW each function executes when each of its inputs become valid thus the order of execution may not always be intuitive.
In addition, this lesson shall also introduce the Formula Node, which allows the programmer to execute a section of code in accordance with a written formula instead of using the functions blocks available in the Block Diagram. In some circumstances this can be used to simplify sections of the Block Diagram or to increase the ease of development.
Case Structures
As noted, the case structure is somewhat analogous to the IF-THEN-ELSE statement. In fact, the programming languages C, C++ and Java contained a SWITCH CASE statement which is a closer analogy to the functionality of LabVIEW’s Case Structure, however these structures have somewhat fallen out of popular usage. In general, the SWITCH CASE statement used the following basic syntax:
Switch (Conditional Control)
{
Case 1:
Block of Code 1;
Break;
Case 2:
Block of Code 2;
Break;
Default:
Block of Code 3;
Break;
}
The SWITCH CASE statement generally tests the value of a variable (the ‘Conditional Control’) and compares it with a set of case identifiers (‘Case 1’, ‘Case 2’, etc.). If the Conditional Control matches a specific case identifier then the block of code associated with that case would be executed. Generally, if the Conditional Control did not match a specific case, a default block of code would be executed instead.
LabVIEW enacts similar functionality in the Case Structure.
The Case Structure contains one or more sub-diagrams within the Block Diagram, one of which executes depending on the value wired into the case selector.
To place a Case Structure, select ‘Structures >> Case Structure’ from the Functions Palette and draw the structure onto the Block Diagram.
By default, the case structure has one sub-diagram for the Boolean TRUE case and a second sub-diagram for the Boolean FALSE case. The TRUE case sub-diagram is shown by default but by clicking the arrows on the Case Name the FALSE case sub-diagram can be brought forward.
Which of these sub-diagrams is then executed by the VI is determined by the value of a Boolean control wired into the Case Selector.
We can illustrate this by creating a simple Case Structure that outputs one of two specific messages to a String Indicator determined by a Boolean Control.
Note that we will be running the VI continuously, so will place the Case Structure inside a WHILE loop terminated by a STOP button.
Create the VI as illustrated, remembering to input a different message into the False Case.
When we then execute this VI (using ‘Run Continuously’), we generate the following pair of states.
When the Case Selector is controlled by a Boolean Control, only two cases are available to the programmer. However, the input data can also be a string, integer, enumerated data type or even an error cluster. The data type you wire to the case selector determines the number of allowed cases you can enter in the selector label.
Exercise 1: Case Structures and Error Handling
In this exercise, you should create a VI that takes two numbers (A and B) and computes the calculation .
- If B is non-zero, the VI should return the result of the calculation.
- If B = 0, the VI should return a value of 999.99 and insert an error into the error cluster.
To create this VI, use a Case Structure, in which the Case Selector is governed by a Boolean Control evaluating whether value B is equal to zero.
A suitable Block Diagram structure is shown below, which uses the following functions (although you may achieve the same functionality using a different method):
- ‘Equal to 0?’ function to govern the Case Selector
- ‘Divide’ function for calculation when valid
- ‘Bundle by Name’ function for error handling
Sequence Structures
In text-based programming languages, the code executes in the order in which it is written, i.e., executing line-by-line through the code. In data flow based programming environment, a function executes when all its inputs become valid. This can sometimes lead to the developer having difficulty determining the exact order in which code is being executed. This can cause errors as it may be required that some events in code take place before other events. In LabVIEW, the Sequence Structure adds control to the order of execution of the VI
The Sequence Structure (‘Structures >> Flat Sequence Structure’ from the Functions Palette) appears in the form of a film strip, each frame of which executes independently only when the one prior to it passes control. The Sequence Structure executes from left to right.
Note, additional ‘frames’ can be added by right-clicking on the border and selecting ‘Add frame after’.
Formula Nodes
When implementing complex mathematical equations, it is sometimes desirable (e.g. for neatness or ease of writing) to use a text-based programming medium instead of creating a complex Block Diagram. The ‘Formula Node’ in LabVIEW allows the programmer to do this.
To place a formula node, select ‘Structures >> Formula Node’ from the Functions Palette and draw the object onto the Block Diagram.
All Input and Output Variables used in the equation need to be specified and the Formula Node frame creates a terminal on its border for each variable. It is with these terminals that data can be passed into or taken from the Formula Node. To add new variables, right-click and select ’Add Input’ or ‘Add Output’ and then name the variable as it is to be used in the formula.
Multiple algebraic equations can be implemented in the same Formula Node, with each line ended with a semi-colon.
Note that variables are case sensitive, and every variable used in a formula needs to be assigned an input or output terminal.
Summary
This lesson has introduced the use of Case Structures describing how they can be used to emulate a conditional statement such the IF-THEN-ELSE statement common to text-based programming languages.
This lesson also provided a brief overview of Sequence Structures and how they might be implemented to control to the order of execution of a VI.
Finally, this lesson gave a brief introduction to the Formula Node, which allows the programmer to execute a section of code in accordance with a written formula instead of using the pre-made functions in the Block Diagram. The syntax used by Formula Nodes is fairly intuitive and similar to many text-based programming languages. Syntax reference material can be found at: