If you've ever programmed in any language, you've probably wanted to repeat the execution of a section of code. LabVIEW offers two loop structures to make this easy. You can use the For Loop or While Loop to control repetitive operations in a VI. A For Loop executes a specified number of times; a While Loop executes until a specified condition is true (or false, depending on how the While Loop is configured). You can find both loops under the Programming>>Structures subpalette of the Functions palette. The For LoopA For Loop executes the code inside its borders, called its subdiagram, for a total of count times, where the count equals the value contained in the count terminal. You can set the count by wiring a value from outside the loop to the count terminal. If you wire 0 to the count terminal, the loop does not execute. Figure 6.1. For Loop
The iteration terminal contains the current number of completed loop iterations: 0 during the first iteration, 1 during the second, and so on, up to N-1 (where N is the number of times you want the loop to execute). The For Loop is equivalent to the following pseudocode:
The While LoopThe While Loop executes the subdiagram inside its borders until the Boolean value wired to its conditional terminal is TRUE (meaning "yes, stop the loop"), as shown in Figure 6.2. LabVIEW checks the conditional terminal value at the end of each iteration. If the value is FALSE (meaning "no, don't stop the loop"), another iteration occurs. Figure 6.2. While Loop
The While Loop's iteration terminal behaves exactly like the one in the For Loop. The While Loop is equivalent to the following pseudocode:
You can also change the state that the conditional terminal of the While Loop checks, so that instead of looping while false, you can have it loop while true. To do this, you pop up on the conditional terminal, and select "Continue if True." The While Loop will look like Figure 6.3. Figure 6.3. While Loop with "Continue if True" conditional terminal
The While Loop in Figure 6.3 is equivalent to the following pseudocode:
As of LabVIEW 7.0, the initial behavior of the While Loop's conditional terminal (when first dropped onto the block diagram of a VI) is Stop if True ( ). However, in LabVIEW 6.1 and earlier, the default state of the conditional terminal is Continue if True ( ). When you are following along with the examples in this book, make sure that you have correctly configured any While Loop conditional terminals to match those in the examples. If you want to convert a structure to a different type (for example, turn a For Loop into a While Loop), you can right-click on the outer edge of the structure and select a Replace item from the shortcut menu. You can only replace a structure with similar structures. Experiment with this feature to see how it works. Placing Objects Inside StructuresFor Loop Cursor While Loop Cursor When you first select a structure from the Programming>>Structures subpalette of the Functions palette, the cursor appears as a miniature of the structure you've selected; for example, the For Loop or the While Loop. You can then click where you want one corner of your structure to be, and drag to define the borders of your structure. When you release the mouse button, the structure will appear, containing all objects you captured in the borders. Once you have the structure on the diagram, you can place other objects inside either by dragging them in, or by placing them inside when you select them from the Functions palette. To make it clear to you that you are dragging something into a structure, the structure's border will highlight as the object moves inside. When you drag an object out of a structure, the block diagram's border (or that of an outer structure) will highlight as the object moves outside. You can resize an existing structure by grabbing and dragging a resizing handle on an edge with the Positioning tool. If you move an existing structure so that it overlaps another object, the overlapped object will be visible above the edge of the structure. If you drag an existing structure completely over another object, that object will display a thick shadow to warn you that the object is over or under rather than inside the structure. Both of these situations are shown in Figure 6.4. Figure 6.4. A subVI that is not inside a structure, floating above it and hiding beneath it
Auto GrowAll structures have a property called Auto Grow, which can be toggled by checking or unchecking the Auto Grow item of the structure's pop-up menu. When enabled, this feature causes two very useful behaviors. First, it causes the structure to grow automatically when you move objects inside the structure, to positions that overlap the structure's border. Second, this feature prevents you from resizing a structure to a size smaller than the objects inside it. Both of these features prevent objects inside the structure from being hidden inside the structure but outside its frame. You can change whether LabVIEW enables this setting for new structures placed onto the block diagram by changing the Place structures with Auto Grow enabled checkbox in the Block Diagram section of the Tools>>Options . . . dialog. Removing Structures: Don't Just Delete ThemWhenever you want to remove a structure, be careful. If you just delete that While Loop, you'll also delete all the objects inside of itfunctions, subVIs, wires, and all. Unless you want to delete all the code inside a structure, you should pop up on the structure and select "Remove While Loop" (for While Loops) or "Remove For Loop" (for For Loops). This will remove the structure, but leave all the other code on the block diagram. It will also leave all wires intact that pass through the frame of the structure via tunnels and shift registers. Terminals Inside Loops and Other Behavioral IssuesData passes into and out of a loop through a little box on the loop border called a tunnel. Because LabVIEW operates according to dataflow principles, inputs to a loop must pass their data in before the loop executes. Loop outputs pass data out only after the loop completes all iterations. Also, according to dataflow, you must place a terminal inside a loop if you want that terminal checked or updated on each loop iteration. For example, the While Loop in Figure 6.5 checks its Boolean control each time it loops. When the loop reads a TRUE value, it terminates. Figure 6.5. Stop button inside a loop (reads the value, every loop iteration)
If you place the terminal of the Boolean control outside the While Loop, as pictured in the right loop of Figure 6.6, you create an infinite loop or a loop that executes only once, depending on the Boolean's initial value. True to dataflow, LabVIEW reads the value of the Boolean before it enters the loop, not within the loop or after completion. Figure 6.6. Stop button outside a loop (reads the value only once, before the loop executes)
Similarly, the Digital Indicator in the loop in Figure 6.7 will update during each loop iteration. The Digital Indicator in the loop in Figure 6.8 will update only once, after the loop completes. It will contain the random number value from the last loop iteration. Figure 6.7. An indicator inside a loop (updated each iteration)
Figure 6.8. An indicator outside a loop (updated only once, on loop completion)
Remember, the first time through a For Loop or a While Loop, the iteration count is zero! If you want to show how many times the loop has actually executed, you must add one to the count! Activity 6-1: Counting the LoopsIn this activity, you get to build a For Loop that displays its count in a chart on the front panel. You will choose the Number of Iterations, and the loop will count from zero up to that number minus one (because the iteration count starts at zero). You will then build a While Loop that counts until you stop it with a Boolean switch. Just for fun (and also to illustrate an important point), you will observe the effect of putting controls and indicators outside the While Loop.
The Coercion DotRemember the little gray dot present at the junction of the For Loop's count terminal and the Number of Iterations wire in the last activity? It's the coercion dot, so named because LabVIEW is coercing one numeric representation to fit another. If you wire two terminals of different numeric representations together, LabVIEW converts one to the same representation as the other. In the previous exercise, the count terminal has a 32-bit integer representation, while the Number of Iterations control is by default a double-precision floating-point number until you change it. In this case, LabVIEW converts the double-precision floating-point number to a long integer. In doing so, LabVIEW makes a new copy of the number in memory, in the proper representation. This copy takes up space. Although the extra space is negligible for scalar numbers (single-valued data types), it can add up quickly if you are using arrays (which store multiple values). Try to minimize the appearance of the coercion dot on large arrays by changing the representation of your controls and indicators to exactly match the representation of data they will carry. To make it easier to identify coercion dots on the block diagram (which encourages you to follow the preceding rule), you can change their color from the default gray, to a more noticeable color such as bright red. To change the color, open the Tools>>Options . . . dialog and uncheck the Use default colors option (in the Colors category). Click on the Coercion Dots color and change it to any color you like, from the color palette that appears. When a VI converts floating-point numbers to integers, it rounds to the nearest integer. A number with a decimal value of ".5" is rounded to the nearest even integer. An easy way to create a count terminal input with the correct data type and representation is to pop up on the count terminal and select Create Constant (for a block diagram constant) or Create Control (for a front panel control). Likewise, you can create indicators in a similar manorfor example, you can pop up on the iteration terminal and select Create Indicator (for a front panel indicator) to view the iteration count as the loop executes. |