Summary: In this lesson, you will learn how to use the Case structure.
Note: Your browser may not currently support MathML. See our browser support page for additional details. You can always view the correct math in the PDF version.
A Case
structure has two or more subdiagrams, or cases. Only one
subdiagram is visible at a time, and the structure executes only
one case at a time. An input value determines which subdiagram
executes. The Case structure is similar to case
statements or if...then...else statements in text-based
programming languages.
The case
selector identifier at the top of the Case
structure contains the case selector identifier in the center
and decrement and increment buttons on each side. Use the
decrement and increment buttons to scroll through the available
cases.
Wire an input
value, or selector, to the selector terminal to
determine which case executes. You must wire an integer, Boolean
value, string, or enumerated type value to the selector
terminal. You can position the selector terminal anywhere on the
left border of the Case structure. If you wire a
Boolean to the selector terminal, the structure has a
True case and a False case. If you
wire an integer, string, or enumerated type value to the
selector terminal, the structure can have up to
You can specify a default case for the Case
structure. You must specify a default case to handle
out-of-range values or explicitly list every possible input
value. For example, if you specified cases for 1, 2, and 3 but
you get an input of 4, the Case structure executes
the default case.
You can specify a default case for the Case
structure. You must specify a default case to
handle out-of-range values or explicitly list every possible
input value. For example, if you specified cases for
1, 2, and 3 but you get
an input of 4, the Case structure executes the
default case.
Right-click the Case structure border to add,
duplicate, remove, or rearrange cases and to select a
default case.
The block diagram in Figure 1 is an example of
the Thermometer VI that uses a
Case structure instead of the Select
function. It is shown with the True case foremost
in the Case structure.
![]() |
To select a case, type the value in the case selector
identifier or use the Labeling tool to edit the
values, as shown in the block diagram in Figure 2.
![]() |
Once you have selected another case, that case appears foremost, as shown in the block diagram in Figure 3.
![]() |
If you enter a selector value that is not the same type as the object wired to the selector terminal, the value appears red to indicate that you must delete or edit the value before the structure can execute, and the VI will not run. Also, because of the possible round-off error inherent in floating-point arithmetic, you cannot use floating-point numeric values as case selector values. If you wire a floating-point value to the case, LabVIEW rounds the value to the nearest even integer. If you type a floating-point value in the case selector, the value appears red to indicate that you must delete or edit the value before the structure can execute.
You can create multiple input and output tunnels for a
Case structure. Inputs are available to all
subdiagrams, but subdiagrams do not need to use each
input. When you create an output tunnel in one case,
corresponding tunnels appear at the same position on the
border in all other cases.
If at least one output tunnel is not defined, all output
tunnels on the structure appear as white squares. Wire to the
output tunnel for each unwired case, clicking the tunnel each
time. You can define a different data source for the same
output tunnel in each case, but the data types must be
compatible. You also can wire constants or controls to unwired
cases by right-clicking the tunnel and selecting
Create>>Constant or Create>>Control
from the shortcut menu.
Use
Default If Unwired from the shortcut menu to use the
default value for the tunnel data type for all unwired
tunnels.
In the following examples, the numeric values pass through
tunnels to the Case structure and are either
added or subtracted, depending on the value wired to the
selector terminal.
The example in Figure 4 is a Boolean
Case structure. The cases are shown overlapped
to simplify the illustration.
![]() |
If the Boolean control wired to the selector terminal is
True, the VI adds the numeric
values. Otherwise, the VI subtracts the numeric values.
The example in Figure 5 is an integer Case structure.
![]() |
Integer is a text ring control located on the
Controls>>Text Controls palette that associates
numeric values with text items. If the text ring control
wired to the selector terminal is 0
(add), the VI adds the numeric values. If the
value is 1 (subtract), the VI
subtracts the numeric values. If the text ring control is
any other value than 0 (add) or
1 (subtract), the VI adds the
numeric values, because that is the default
case.
The example in Figure 6 is a string
Case structure.
![]() |
If String is add, the VI adds the
numeric values. If String is
subtract, the VI subtracts the numeric values.
The example in Figure 7 is an enumerated
Case structure.
![]() |
An enumerated control gives users a list of items from which
to select. The data type of an enumerated control includes
information about the numeric values and string labels in
the control. When you wire an enumerated control to the
selector terminal of a Case structure, the case
selector displays a case for each item in the enumerated
control. The Case structure executes the
appropriate case subdiagram based on the current item in the
enumerated control. In the block
diagram, if Enum is add,
the VI adds the numeric values. If Enum is
subtract, the VI subtracts the numeric values.
The example in Figure 8 is an error cluster
Case structure.
![]() |
When you wire an error cluster to the selector terminal of a
Case structure, the case selector label
displays two cases, Error and No
Error, and the border of the Case
structure changes color--red for Error and
green for No Error. The Case
structure executes the appropriate case subdiagram based on
the error state.
When an error cluster is wired to the selection
terminal, the Case structure recognizes only
the status Boolean of the cluster.
"A full introductory course on programming with LabVIEW."