Execution Structures in LabVIEW

Included in the Section

Execution structures contain sections of graphical code and control how and when the code inside is run. The most common execution structures are While Loops, For Loops and Case structures which you can use to run the same section of code multiple times or to execute a different section of code based on some condition.

Loops

While Loops

Similar to a Do Loop or a Repeat-Until Loop in text-based programming languages, a While Loop, shown in Figure 1, executes the code it contains until a condition occurs.

(1) LabVIEW While Loop | (2) Flowchart | (3) Pseudo Code

Figure 1 shows a While Loop in LabVIEW, a flowchart equivalent of the While Loop functionality, and a pseudo code example of the functionality of the While Loop.

The While Loop is located on the Structures palette. Select the While Loop from the palette and then use the cursor to drag a selection rectangle around the section of the block diagram you want to repeat. When you release the mouse button, a While Loop boundary encloses the section you selected.

Add block diagram objects to the While Loop by dragging and dropping them inside the While Loop.

The While Loop executes the code it contains until the conditional terminal, an input terminal, receives a specific Boolean value.

You also can perform basic error handling using the conditional terminal of a While Loop. When you wire an error cluster to the conditional terminal, only the True or False value of the status parameter of the error cluster passes to the terminal. Also, the Stop if True and Continue if True shortcut menu items change to Stop if Error and Continue while Error.

The iteration terminal is an output terminal that contains the number of completed iterations.

The iteration count for the While Loop always starts at zero.

Note: The While Loop always executes at least once.

Infinite Loops

Infinite loops are a common programming mistake that involves a loop that never stops.

If the conditional terminal is Stop if True, you place the terminal of a Boolean control outside a While Loop. If the control is FALSE when the loop starts, you cause an infinite loop.

Figure 2. Boolean Control Outside the While Loop

Changing the value of the control does not stop the infinite loop because the value is read only once, and that happens before the loop starts. To use a control to stop a While Loop, you should place the control terminal inside the loop. To stop an infinite loop, you must abort the VI by clicking the Abort Execution button on the toolbar.

In Figure 3 the While Loop executes until the Random Number function output is greater than or equal to 10.00 and the Enable control is True. The And function returns True only if both inputs are True. Otherwise, it returns False.

In Figure 3, there is an infinite loop since the random function never generates a value equal to or greater than 10.0 0.

Figure 3. Infinite Loop

Structure Tunnels

Tunnels feed data into and out of structures. The tunnel appears as a solid block on the border of the While Loop. The block is the color of the data type wired to the tunnel. Data passes out of a loop after the loop terminates. When a tunnel passes data into a loop, the loop executes only after the data arrives at the tunnel.

In Figure 4, the iteration terminal is connected to a tunnel. The value in the tunnel does not get passed to the Iterations indicator until the While Loop finishes executing.

Figure 4. While Loop Tunnel

Only the last value of the iteration terminal displays in the Iterations indicator.

For Loops

released with niBCVideo v1.1

Video: Using For Loops in LabVIEW

A For Loop executes a subdiagram a set number of times. Figure 5 shows a For Loop in LabVIEW, a flowchart equivalent of the For Loop functionality, and a pseudo code example of the functionality of the For Loop.

(1) LabVIEW For Loop | (2) Flowchart | (3) Pseudo Code

Figure 5. For Loop

The For Loop is on the Structures palette. You also can place a While Loop on the block diagram, right-click the border of the While Loop, and select Replace with For Loop from the shortcut menu to change a While Loop to a For Loop.

The count terminal is an input terminal whose value indicates how many times to repeat the subdiagram.

The iteration terminal is an output terminal that contains the number of completed iterations.

The iteration count for the For Loop always starts at zero.

The For Loop differs from the While Loop in that the For Loop executes a set number of times. A While Loop stops executing only if the value at the conditional terminal exists.

The For Loop in Figure 6 generates a random number every second for 100 seconds and displays the random numbers in a numeric indicator.

Figure 6. For Loop Example

Adding Timing to Loops

When a loop finishes executing an iteration, it immediately begins executing the next iteration, unless it reaches a stop condition. Most often, you need to control the iteration frequency or timing. For example, if you are acquiring data, and you want to acquire the data once every 10 seconds, you need a way to time the loop iterations so they occur once every 10 seconds. Even if you do not need the execution to occur at a certain frequency, you need to provide the processor with time to complete other tasks, such as responding to the user interface.

Wait Function

Place a Wait function inside a loop to allow a VI to sleep for a set amount of time. This allows your processor to address other tasks during the wait time. Wait functions use the millisecond clock of the operating system.

The Wait (ms) function waits until the millisecond counter counts to an amount equal to the input you specify. This function guarantees that the loop execution rate is at least the amount of the input you specify.

Case Structures

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 switch statements or if...then...else statements in text-based programming languages.

The case selector label at the top of the Case structure contains the name of the selector value that corresponds to the case in the center and decrement and increment arrows on each side.

Click the decrement and increment arrows to scroll through the available cases. You also can click the down arrow next to the case name and select a case from the pull-down menu.

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 the data type of the selector terminal is Boolean, the structure has a True case and a False case. If the selector terminal is an integer, string, or enumerated type value, the structure can have any number of cases.

Note: By default, string values you wire to the selector terminal are case sensitive. To allow case-insensitive matches, wire a string value to the selector terminal, right-click the border of the Case structure, and select Case Insensitive Match from the shortcut menu.

If you do not specify a default case for the Case structure to handle out-of-range values, you must explicitly list every possible input value. For example, if the selector is an integer 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.

Note: You cannot specify a default case if you wire a Boolean control to the selector. If you right-click the case selector label, Make This The Default Case does not appear in the shortcut menu. Make the Boolean control TRUE or FALSE to determine which case to execute.

Right-click the Case structure border to add, duplicate, remove, or rearrange cases, and to select a default case.

Selecting a Case

Figure 7 shows a VI that uses a Case structure to execute different code depending on whether a user selects °C or °F for temperature units. The top block diagram shows the True case in the foreground. In the middle block diagram, the False case is selected. To select a case, enter the value in the case selector identifier or use the Labeling tool to edit the values. After you select another case, that case displays on the block diagram, as shown in the bottom block diagram of Figure 7.

Figure 7. Changing the Case View of a Case Structure

If you enter a selector value that is not the same type as the object wired to the selector terminal, the value appears red. This indicates that the VI will not run until you delete or edit the value. Also, because of the possible round-off error inherent in floating-point arithmetic, you cannot use floating-point numbers as case selector values. If you wire a floating-point value to the case, LabVIEW rounds the value to the nearest integer. If you type a floating-point value in the case selector label, 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 cases, but cases do not need to use each input. However, you must define an output tunnel for each case.

Consider the following example: a Case structure on the block diagram has an output tunnel, but in at least one of the cases, there is no output value wired to the tunnel. If you run this case, LabVIEW does not know what value to return for the output. LabVIEW indicates this error by leaving the center of the tunnel white. The unwired case might not be the case that is currently visible on the block diagram.

To correct this error, display the case(s) that contain(s) the unwired output tunnel and wire an output to the tunnel. You also 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. When the output is wired in all cases, the output tunnel is a solid color.

Avoid using the Use Default If Unwired option. Using this option does not document the block diagram well, and can confuse other programmers using your code. The Use Default If Unwired option also makes debugging code difficult. If you use this option, be aware that the default value used is the default value for the data type that is wired to the tunnel. For example, if the tunnel is a Boolean data type, the default value is FALSE.

Data Type

Default Value

Numeric

0

Boolean

FALSE

String

empty (“”)

Table 1. Data Type Default Values

Other Structures

LabVIEW has other, more advanced types of execution structures such as Event structures (used to handle interrupt-driven tasks like UI interaction) and Sequence structures (used to force execution order), which are out of the scope of this introductory material. To learn more about these structures, refer to the appropriate LabVIEW Help topic.