Two Loops


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 Loop

A 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:

for i = 0 to N-1       Execute subdiagram 



The While Loop

The 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:

Do       Execute subdiagram While condition is FALSE (Until condition is TRUE) 



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:

Do       Execute subdiagram While condition is NOT TRUE 



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 Structures

For 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 Grow

All 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 Them

Whenever 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 Issues

Data 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 Loops

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

1.

Create a new panel by selecting New VI from the File menu or by clicking the New VI text in the LabVIEW Getting Started dialog box.

2.

Build the front panel and block diagram shown in Figures 6.9 and 6.10. The For Loop is located in the Programming>>Structures subpalette of the Functions palette. You might use the Tile Left and Right command from the Windows menu so that you can see both the front panel and the block diagram at the same time.

Figure 6.9. The front panel of the VI you will build during this activity


Figure 6.10. The block diagram of the VI you will build during this activity


Drop a Waveform Chart from the Modern>>Graph subpalette of the Controls palette onto your front panel. Label it For Loop Count. We'll talk more about charts and graphs in Chapter 8, "LabVIEW's Exciting Visual Displays: Charts and Graphs." Use a digital control from the Programming>>Numeric subpalette for your Number of Iterations control.

3.

Pop up on the Waveform Chart and select AutoScale Y from the Y Scale pull-out menu so that your chart will scale to fit the For Loop count. Then pop up on the chart and Visible Items>>Digital Display. Input a number to your Number of Iterations control and run the VI. Notice that the digital indicator counts from 0 to N-1, NOT 1 to N (where N is the number you specified)! Each time the loop executes, the chart plots the For Loop count on the Y axis against time on the X axis. In this case, each unit of time represents one loop iteration.

4.

Notice the little gray dot present at the junction of the count terminal and the Number of Iterations wire. It's called a coercion dot, and we'll talk about it after this exercise. Pop up on the Number of Iterations control and choose I32 Long from the subpalette to make it go away.

5.

You can save the VI if you want, but we won't be using it again. Open up another new window so you can try out the While Loop.

6.

Build the VI shown in Figures 6.11 and 6.12. Remember, Booleans appear on the front panel in their default FALSE position. Also, remember to set the While Loop's conditional terminal to Continue if True.

Figure 6.11. The front panel of the VI you will build


Figure 6.12. The block diagram of the VI you will build


7.

Flip the switch up to its TRUE position by clicking on it with the Operating tool and run the VI. When you want to stop, click on the switch to flip it down to FALSE. Loop Count will update during each loop iteration.

Operating Tool

8.

With the switch still in the FALSE position, run the VI again. Notice that the While Loop executes once, but only once. Remember, the loop checks the conditional terminal at the end of an iteration, so it always executes at least once, regardless of what value is wired to it.

9.

Now go to the block diagram and move the Loop Count indicator outside the loop as shown in Figure 6.13. You will have to rewire the indicator; the tunnel is created automatically as the wire leaves the loop.

Figure 6.13. Your VI, after moving the Loop Count indicator outside the While Loop


10.

Make sure the switch is TRUE and run the VI. Notice that the indicator updates only after you flip the switch FALSE and the loop has finished executing; it contains the final value of the iteration count, which is passed out after the loop completes. You will learn more about passing data out of loops in Chapter 7, "LabVIEW's Composite Data: Arrays and Clusters." Until then, do not try to pass scalar data out of a For Loop like you just did in a While Loop, or you will get bad wires and you won't understand why. It can easily be done, but you will have to learn a little about auto-indexing first.

11.

Save the VI. Place it in your MYWORK directory and call it Loop Count.vi.

12.

Now, just to demonstrate what not to do, drag the switch out of the loop (but leave it wired). Make sure the switch is TRUE, run the VI, and then hit the switch to stop it. It won't stop, will it? Once LabVIEW enters the loop, it will not check the value of controls outside of the loop (just like it didn't update the Loop Count indicator until the loop completed). Go ahead and hit the Abort button on the Toolbar to halt execution. If your switch had been FALSE when you started the loop, the loop would have only executed once instead of forever. Close the VI and do not save changes.

Abort Button

The Coercion Dot

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





LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net