Connexions

You are here: Home » Content » Case Structures
Content Actions
Lenses

What is a lens?

Lenses

A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual Connexions member, a community, or a respected organization.

This content is ...
Affiliated with (?)
This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • This module is included in aLens by: National InstrumentsAs a part of collection:"LabVIEW Graphical Programming Course"

    Comments:

    "A full introductory course on programming with LabVIEW."

    Click the "National Instruments" link to see all content affiliated with them.

    National Instruments
Also in these lenses
  • This module is included inLens: Connexions Books Available for Print on Demand
    By: ConnexionsAs a part of collection:"LabVIEW Graphical Programming Course"

    Comments:

    "This book is available through the Connexions beta version of print-on-demand from online materials."

    Click the "Printable Books" link to see all content selected in this lens.

    Printable Books
Tags

(?)

These tags come from the endorsement, affiliation, and other lenses that include this content.

Case Structures

Module by: National Instruments

Summary: In this lesson, you will learn how to use the Case structure.

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 231-1 2 31 1 cases.
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.

Selecting a 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.
Figure 1
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.
Figure 2
Once you have selected another case, that case appears foremost, as shown in the block diagram in Figure 3.
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.

Input and Output Tunnels

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.
Note: You can right-click the output tunnel and select Use Default If Unwired from the shortcut menu to use the default value for the tunnel data type for all unwired tunnels.

Examples

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.
Example 1: Boolean Case Structure 
The example in Figure 4 is a Boolean Case structure. The cases are shown overlapped to simplify the illustration.
Figure 4
If the Boolean control wired to the selector terminal is True, the VI adds the numeric values. Otherwise, the VI subtracts the numeric values.
Example 2: Integer Case Structure 
The example in Figure 5 is an integer Case structure.
Figure 5
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.
Example 3: String Case Structure 
The example in Figure 6 is a string Case structure.
Figure 6
If String is add, the VI adds the numeric values. If String is subtract, the VI subtracts the numeric values.
Example 4: Enumerated Case Structure 
The example in Figure 7 is an enumerated Case structure.
Figure 7
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.
Example 5: Error Case Structure 
The example in Figure 8 is an error cluster Case structure.
Figure 8
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.

Comments, questions, feedback, criticisms?

Discussion forum

Send feedback