Execution Structures in NI LabVIEW

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.


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 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.

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.

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 will never generate a value greater than 10.0.

Figure 3. Infinite Loop


For Loops

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 located 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.

Case Structures

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 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.

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

The first example shows a Boolean input to the case selector with two cases, True and False, available to be executed. The second example shows a numeric input, with the cases labeled 0,1 and 2 and a default (assigned to be the same as the 0 case). If the case structure has an output terminal then each case must assign a value (of the same data-type) to that terminal.

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.

  • Example: Function Generator

In this exercise we will build a function generator from the step-by-step instructions that follow. At the end:

1- Open LabVIEW which can be found in the Start Menu as “National Instruments LabVIEW” or something closely related. Once that is open choose, “New VI” from the window.

2- Put down a digital control from the controls palette. This can be done by Controls  Num Ctrls  Num Ctrl then click on the Front Panel Window. Now type Function Number before doing anything else. This will save us some work in the future. Once this is done you should have a window like that of Figure 4.

Figure 4: Front panel after Step 2

3- Right click on the new control that you just placed and select Format & Precision from the menu. This will give a new window titled “Format & Precision” like that of Figure 5. Change the dropdown selection from Significant Digits to Digits of Precision and enter 0 (zero) in the corresponding field. Click OK.

Figure 5: Format and Precision dialog window

4- At this point it is time to put down some more controls. Find the Dial under the controls palette, or using Controls Num Ctrls  Dial. Place this on the Front Panel Window and type Amplitude. This will be the amplitude of the waveforms.

5- Place another dial in the same manner and type Frequency. This will be the frequency of the waveforms.

6- Now, under the Graph part, place a Waveform Graph. This can be found under Controls  Graph Inds  Graph and type in Waveform Output before doing anything further. Right click on the Waveform Chart and select Visible Items  Plot Legend from the menu. This will remove the Plot Legend from the chart. After this the Front Panel should look like that of Figure 6.

Figure 6: Completed Front Panel after adding the Waveform Graph

7- This will take care of the Front Panel for now. Now switch to the block diagram window by choosing Window  Show Block Diagram from the menu on the top of the screen. This will open a window like that of Figure 7.

Figure 7: LabVIEW Block Diagram Window

8- Right click on the orange box for the Function Number and choose Representation  I8 from the pop up menu. This will change the indicator from a double precision variable to an integer value.

9- The next step will be to draw a case selection box. This can be done by choosing Functions  All Functions  Structures  Case. Now click and drag from somewhere near the amplitude box to somewhere down towards the right of the screen. You should get something that looks like Figure 8 below. (Later in the lab, you will be asked to explain what the case statement does. For now, the case selection box can be thought of as a graphical representation of a case statement in a programming language.)

Figure 8: Diagram window before adding the waveforms

10- Right click on where the case box says “True” and select Add Case After. Enter 1 from the key board. Repeat this 2 more times entering 2 and 3 from the keyboard. Now, click on the “selector label” between the two arrows and go back to one of the two cases (“True” or “False”). Then right click again and select Delete This Case from the menu. Repeat this for the “False” (or “True”) case also.

11- Then to make the case selection work as designed, we need to give it some value that it can select from. Again, the mouse is context sensitive and changes to suit what it thinks you want to do. When its on a connector, like one of the little triangle connectors, it will change to something that looks like a little spool of wire. If you left click on the mouse when it is on top of one of the connectors, the connector will be highlighted. Hold the button down and drag the wire to the other connector. Click on the blue box below Function Number and drag to the little question mark on the side of the Case Selection Box.

12- Before continuing, make sure that case “1” is in the little box at the top of the Case Selection Box. Now right click between the arrows and choose Make this the Default Case from the menu. This sets case 1 as the default if no case is selected. When done you should have something that looks like Figure 9.

Figure 9: Diagram window after case additions and wire

13- From here we have to add the specific waveforms. Under case 1, we are going to put a sine wave. This can be found under Functions  Analysis  Simulate Sig. Place this in the middle of the case selection box. You can double click on this component at any time to configure its properties but as soon as you drop the icon inside the case statement the Configure Simulate Signal Window will pop up whether you want it to or not. In the Configure Simulate Signal Window:

a. Under Timing, set Samples/second (Hz) to 1000

b. Next to Number of Samples, uncheck the Automatic checkbox.

c. Set Number of Samples to 1000.

These settings will set the x-axis to 1 second.

14- Case 2 will be a square wave. Switch to case 2 by clicking on the arrow on the right on top of the case selection box. Inside of case 2, place another Simulate Sig component in the middle of the case selection box. In the Configure Simulate Signal window, change the wave type to Square. Make the same changes as described in 13a-c.

15- Case 3 will be a triangle wave. Switch to case 3. By now you should be able to figure out how to add the triangle wave simulator. (It’s just like the last two steps!) You should end up with a block diagram window that looks like Figure 10.

Figure 10: Diagram window after adding the waveforms to the Case Selection Box

16- Wire the output of the Frequency function to the ‘Frequency’ input connector of the sine wave. Then wire the amplitude box to the ‘amplitude’ of the sine wave. Then wire the ‘sine wave’ output to the Waveform Output box. Check this against Figure 11 to verify that it is correct.

Figure 11: Completed Diagram Window after wiring the Sine Wave Function

17- Repeat this same process for the other two cases also. This exact wiring scheme works for all three functions.

18- Return to the Front Panel Window and click the ‘Run Continuously’ button. This button looks like two arrows chasing each other. Now you can change the values of Amplitude, Frequency, and Function Number. Notice that if Function Number is less than 1 or greater than 3 the waveform defaults back to the sine wave.

Figure 11: Completed Front Panel Window continuously executing the Triangle Function

  • Exercise

Create a simple Case Structure Application to convert degree from Celsius to Fahrenheit and opposite VI. There is a Boolean control applied to the selector terminal, so the Case Structure is the Boolean data type. When he Boolean control on the Front Panel is in the up position, it returns a FALSE to the selector terminals which invoke the Celsius to Fahrenheit case.

Note use formula structure.