Saturday, September 25, 2010

Case Structures

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



case.png 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.
caseid.png 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.
selectcase.png 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 2311 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 12, 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 Casestructure.
Figure 1
Figure 1 (thermocaseT.png)
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
Figure 2 (thermocasechoose.png)
Once you have selected another case, that case appears foremost, as shown in the block diagram in Figure 3.
Figure 3
Figure 3 (thermocaseF.png)
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
Figure 4 (boolcase.png)
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
Figure 5 (intcase.png)
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
Figure 6 (stringcase.png)
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
Figure 7 (enumcase.png)
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
Figure 8 (errorcase.png)
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 Casestructure 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.

Content actions

GIVE FEEDBACK:

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Popular Projects

My Blog List

Give support

Give support
Encourage Me through Comments & Followers

Followers