Loops and case statements of text-based programming languages are represented as structures in graphical programming. Repetition and loop are used to perform an action frequently with variations in the details each time. LabVIEW consists of FOR Loop and WHILE Loop. These loops are used to control repetitive operations. Structures on the block diagram are used to repeat blocks of code and to execute code conditionally or in a specific order. LabVIEW includes structures like the While Loop, For Loop, Case structure, Stacked Sequence structure, Flat Sequence structure, Event structure, and Formula Node. This chapter introduces the loops in LabVIEW, iterative data transfer in loops and timing a loop along with functions commonly used with these structures, including the shift register and the Feedback Node.
A For Loop executes a subdiagram, a set number of times. Figure 4.1(a) shows a For Loop in LabVIEW and Figure 4.1(b) shows the flow chart equivalent of the For Loop functionality. The For Loop is located on the Functions>>Programming>>Structures Palette. Select the For Loop from the palette and use the cursor to drag a selection rectangle to create a new For Loop or around the section of the block diagram you want to repeat. 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.
Both the count and iteration terminals are 32-bit signed integers. If you wire a floating-point number to the count terminal, LabVIEW rounds it and coerces it to within range. If you wire 0 or a negative number to the count terminal, the loop does not execute and the outputs contain the default data for that data type. A For Loop can only execute an integer a number of times.
Example 1: Create a VI using For Loop which changes the color of a color box automatically for the given number of iterations as shown in Figure 4.3.
Solution: The front panel of this VI consists of a Numeric Control for providing number of iterations and a Framed Color Box for displaying various colors as shown in the figure. The framed Color Box indicator is located in Controls>>Modern>>Numeric palette.
In the block diagram the function RGB to Color is used to produce various colors based on the combination of values given to the red, green and blue terminals. The RGB to Color function is located in Functions>>Programming>>Numeric>>Conversion palette.
Subscribe to our youtube channel to get new updates..!
As given in the solution, the For Loop will execute for three iterations. The value of the iteration terminal added with 1 is given as input for one of the terminals of multiplication functions for each iteration. The iteration value is multiplied with 50, 100 and 150 for producing red, green and blue values respectively. These values are given to the RGB to Color function. This RGB to Color function produces three different colors based on the red, green and blue values. The produced colors are displayed in the framed Color Box indicator. The For Loop will execute only for three iterations since the number of iterations is set to 3. The number of iterations can be increased to produce more numbers of colors.
A While Loop executes a subdiagram until a condition is met. The While Loop is similar to a Do Loop or a Repeat-Until Loop in text-based programming languages. Figure 4.4(a) shows a While Loop in LabVIEW and 4.4(b) is the flow chart equivalent of the While Loop. The While Loop always executes at least once. The For Loop differs from the While Loop in that the For Loop executes a set number of times. A While Loop stops executing the subdiagram, only if the expected value at the conditional terminal exists.
In LabVIEW, the WHILE Loop is located on the Functions>>Programming>>Structures palette. You also can place a For Loop on the block diagram, right-click the border of the For Loop, and select Replace with While Loop from the shortcut menu to change a For Loop to a While Loop. The While Loop contains two terminals, namely Conditional Terminal and Iteration Terminal. The Conditional Terminal is used to control the execution of the loop, whereas the Iteration Terminal is used to know the number of completed iterations.
Conventional programming languages support two types of WHILE constructs as illustrated in Figure 4.5. These are called pre- and post-test modes. In the pre-test mode the condition is tested prior to the execution of every iteration and if the result is false, then the execution of the loop is aborted. In the post-test mode the test is carried out only at the end of the loop. Functionally, the major difference is that under the post-test mode even if the condition is false at the first execution or first iteration, the loop will be executed at least once, since the test is only performed at the end of the loop. LabVIEW supports only the post-test form of the While construct.
The While Loop executes the subdiagram until the conditional terminal, and receives a specific Boolean value. The default behavior and appearance of the conditional terminal is Stop if True as shown in Figure 4.6(a). When a conditional terminal is Stop if True, the While Loop executes its subdiagram until the conditional terminal receives a TRUE value. You can change the behavior and appearance of the conditional terminal by right-clicking the terminal or the border of the While Loop and selecting Continue if True from the shortcut menu as shown in Figure 4.6b). When a conditional terminal is Continue if True, the While Loop executes its subdiagram until the conditional terminal receives a FALSE value. You also can use the Operating Tool to click the conditional terminal to change the condition. The VI shows error if the conditional terminal is unwired.
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 ‘i’ (an output terminal), contains the number of completed iterations. The iteration count always starts at zero. During the first iteration, the iteration terminal returns 0. Figure 4.7 shows a simple While Loop which generates 10 random numbers and displays in the Random Number Indicator. The block diagrams shown in Figures 4.7(a) and 4.7(b) consist of a random number generator function. The While Loop will execute for 10 iterations (where ‘i’ value ranges from 0 to 9). A random number is generated and displayed for each iteration in the Numeric Indicator in the Front Panel.
In the block diagram shown in Figure 4.7(a), the conditional terminal used is Stop if True. When the iteration value ‘i’ reaches 9, the conditional terminal receives a TRUE state and stops executing the loop. In the block diagram shown in Figure 4.7(b), the conditional terminal used is Continue if True. For the iteration value ‘i’ ranges from 0 to 8, the conditional terminal receives a TRUE state and keeps the loop executing. When the iteration value becomes 9, the conditional terminal receives a FALSE state and stops executing the loop.
Example 2: Create a VI to display the numbers 1 to 10 in a Numeric Indicator using a While Loop. Use the conditional terminal Stop if True as in Figure 4.8.
Solution: The front panel consists of a Numeric Indicator to display the numbers. In the block diagram the While Loop executes for 10 iterations where the iteration value ‘i’ ranges from 0 to 9. When the iteration value reaches 9, the conditional terminal receives a TRUE state and stops the execution of the While Loop. The iteration value is added with 1 to produce output from 1 to 10 and it is displayed in the Numeric Indicator.