Table Of Contents

Executing Code Based on a Condition

Version:
    Last Modified: March 8, 2017

    Use a Case Structure to incorporate decision logic into your program. A Case Structure contains one or more subdiagrams, or cases, exactly one of which executes when the structure executes. Case Structures behave similarly to switch statements or if-then-else statements in other programming languages.

    What to Use

    • Case Structure
      spd-note-tip
      Tip  

      If you are choosing between only two values based on a Boolean input, you can use the Select node instead of a Case Structure with a Boolean selector.

    What to Do

    Create the following diagram to execute different code based on a given condition.

    Customize the gray sections for your unique programming goals.

    Wire the data that you want to use to make a decision to the selector terminal.

    The data type of the value you wire to the selector terminal defines the various cases that are available in the Case Structure. The selector terminal accepts Boolean, string, integer, floating point, enumerated type, and error cluster data.

    Use the case selector label to define the various cases in which different code executes.

    The case name displayed in the case selector label matches the selector terminal value(s) for which the corresponding subdiagram executes. You can define each case using a single value or a range of values. You can also use the case selector label to specify a default case.

    Place the code you want to execute in each case on the corresponding subdiagram of the Case Structure.

    To add, duplicate, or delete cases, right-click the border of the Case Structure and select the desired option from the Cases shortcut menu.

    To view the different subdiagrams in the Case Structure, click the down arrow on the case selector label and select the desired case.

    Every case must provide data to all output tunnels in order for the Case Structure to execute.

    If any case does not provide data to an output tunnel, an error occurs, and the output tunnel appears as a white box with a colored border.

    To resolve this error and allow the Case Structure to execute without explicitly wiring all cases, right-click the unwired output tunnel and select Default If Unwired. The output tunnel returns the default value for its data type if the subdiagram that executes does not provide that output tunnel with data. When set to Default If Unwired, the output tunnel appears as a white box with a colored border and a dash in the center.

    When all cases provide data to a particular output tunnel, that output tunnel appears as a solid box.

    Troubleshooting

    • If the default case executes unexpectedly, verify that the input values wired to the selector terminal match the values in the case selector label exactly.
    • An edit-time error occurs when there are values of the selector data type that do not correspond to any subdiagram in the Case Structure. You must either define a default case to handle out-of-range values or create a case for every possible input value. For example, if the selector is an integer data type and you specify cases for 1, 2, and 3, you must specify a default case to execute if the input value is 4 or any other unspecified integer value.

    Examples

    Use Search to find and open the following installed lesson: Case Structures


    Recently Viewed Topics