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.
If you would like to Enrich your career with an Android certified professional, then visit Mindmajix - A Global online training platform: “LabVIEW training” Course.This course will help you to achieve excellence in this domain.
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.
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.
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.
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
|LabVIEW Training||Dec 09 to Dec 24||View Details|
|LabVIEW Training||Dec 12 to Dec 27||View Details|
|LabVIEW Training||Dec 16 to Dec 31||View Details|
|LabVIEW Training||Dec 19 to Jan 03||View Details|
I am Ruchitha, working as a content writer for MindMajix technologies. My writings focus on the latest technical software, tutorials, and innovations. I am also into research about AI and Neuromarketing. I am a media post-graduate from BCU – Birmingham, UK. Before, my writings focused on business articles on digital marketing and social media. You can connect with me on LinkedIn.
Copyright © 2013 - 2023 MindMajix Technologies