Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

For Loop and While Loop Structures

LabVIEW 8.2 Help
August 2006

NI Part Number:
371361B-01

»View Product Info

Use the For Loop and the While Loop to control repetitive operations.

Note   If you open a refnum inside a For Loop or While Loop, close that refnum for each iteration of the loop because LabVIEW repeatedly allocates memory for the refnum and does not free the memory until the VI finishes running.

For Loops

A For Loop, shown as follows, executes a subdiagram a set number of times.

The value in the count terminal (an input terminal), shown as follows, indicates how many times to repeat the subdiagram.

Set the count explicitly by wiring a value from outside the loop to the left or top side of the count terminal, or set the count implicitly with auto-indexing.

The iteration terminal (an output terminal), shown as follows, contains the number of completed iterations.

The iteration count always starts at zero. During the first iteration, the iteration terminal returns 0.

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.

Add shift registers to the For Loop to pass data from the current iteration to the next iteration.

While Loops

Similar to a Do Loop or a Repeat-Until Loop in text-based programming languages, a While Loop, shown as follows, executes a subdiagram until a condition occurs.

The While Loop executes the subdiagram until the conditional terminal, an input terminal, receives a specific Boolean value. The default behavior and appearance of the conditional terminal is Stop if True, shown as follows.

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, shown as follows, from the shortcut menu.

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.

If you place the terminal of the Boolean control outside the While Loop, as shown in the following figure, and the control is set to FALSE if the conditional terminal is Stop if True when the loop starts, you cause an infinite loop. You also cause an infinite loop if the control outside the loop is set to TRUE and the conditional terminal is Continue if True.

Changing the value of the control does not stop the infinite loop because the value is only read once, before the loop starts. To stop an infinite loop, you must abort the VI by clicking the Abort Execution button on the toolbar.

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 (an output terminal), shown as follows, contains the number of completed iterations.

The iteration count always starts at zero. During the first iteration, the iteration terminal returns 0.

Add shift registers to the While Loop to pass data from the current iteration to the next iteration.

Controlling Timing

You might want to control the speed at which a process executes, such as the speed at which data values are plotted to a chart. You can use a Wait function in the loop to wait an amount of time in milliseconds before the loop re-executes.

Auto-Indexing Loops

If you wire an array to a For Loop or While Loop input tunnel, you can read and process every element in that array by enabling auto-indexing.

When you wire an array to an input tunnel on the loop border and enable auto-indexing on the input tunnel, elements of that array enter the loop one at a time, starting with the first element. When auto-indexing is disabled, the entire array is passed into the loop. When you auto-index an array output tunnel, the output array receives a new element from every iteration of the loop. Therefore, auto-indexed output arrays are always equal in size to the number of iterations. For example, if the loop executes 10 times, the output array has 10 elements. If you disable auto-indexing on an output tunnel, only the element from the last iteration of the loop passes to the next node on the block diagram.

Right-click the tunnel at the loop border and select Enable Indexing or Disable Indexing from the shortcut menu to enable or disable auto-indexing. Auto-indexing for While Loops is disabled by default.

A bracketed glyph appears on the loop border to indicate that auto-indexing is enabled. The thickness of the wire between the output tunnel and the next node also indicates the loop is using auto-indexing. The wire is thicker when you use auto-indexing because the wire contains an array, instead of a scalar.

The loop indexes scalar elements from 1D arrays, 1D arrays from 2D arrays, and so on. The opposite occurs at output tunnels. Scalar elements accumulate sequentially into 1D arrays, 1D arrays accumulate into 2D arrays, and so on.

Auto-Indexing to Set the For Loop Count

If you enable auto-indexing on an array wired to a For Loop input terminal, LabVIEW sets the count terminal to the array size so you do not need to wire the count terminal. Because you can use For Loops to process arrays an element at a time, LabVIEW enables auto-indexing by default for every array you wire to a For Loop. Disable auto-indexing if you do not need to process arrays one element at a time.

If you enable auto-indexing for more than one tunnel or if you wire the count terminal, the count becomes the smaller of the choices. For example, if two auto-indexed arrays enter the loop, with 10 and 20 elements respectively, and you wire a value of 15 to the count terminal, the loop executes 10 times, and the loop indexes only the first 10 elements of the second array. As another example, if you plot data from two sources on one graph and you want to plot the first 100 elements, wire 100 to the count terminal. If one of the data sources includes only 50 elements, the loop executes 50 times and indexes only the first 50 elements. Use the Array Size function to determine the size of arrays.

Auto-Indexing with While Loops

If you enable auto-indexing for an array entering a While Loop, the While Loop indexes the array the same way a For Loop does. However, the number of iterations a While Loop executes is not limited by the size of the array because the While Loop iterates until a specific condition occurs. When a While Loop indexes past the end of the input array, the default value for the array element type passes into the loop. You can prevent the default value from passing into the While Loop by using the Array Size function. The Array Size function indicates how many elements are in the array. Set up the While Loop to stop executing when it has iterated the same number of times as the array size.

Caution  Because you cannot determine the size of the output array in advance, enabling auto-indexing for the output of a For Loop is more efficient than with a While Loop. Iterating too many times can cause your system to run out of memory.

Using Loops to Build Arrays

In addition to using loops to read and process elements in an array, you also can use the For Loop and the While Loop to build arrays. Wire the output of a VI or function in the loop to the loop border. If you use a While Loop, right-click the resulting tunnel and select Enable Indexing from the shortcut menu. On the For Loop, indexing is enabled by default. The output of the tunnel is an array of every value the VI or function returns after each loop iteration.

Refer to the labview\examples\general\arrays.llb for examples of building arrays.

Shift Registers and the Feedback Node in Loops

Use shift registers or the Feedback Node with For Loops or While Loops to transfer values from one loop iteration to the next.

Shift Registers

Use shift registers when you want to pass values from previous iterations through the loop to the next iteration. A shift register appears as a pair of terminals, shown as follows, directly opposite each other on the vertical sides of the loop border.

The terminal on the right side of the loop contains an up arrow and stores data on the completion of an iteration. LabVIEW transfers the data connected to the right side of the register to the next iteration. After the loop executes, the terminal on the right side of the loop returns the last value stored in the shift register.

Create a shift register by right-clicking the left or right border of a loop and selecting Add Shift Register from the shortcut menu.

A shift register transfers any data type and automatically changes to the data type of the first object wired to the shift register. The data you wire to the terminals of each shift register must be the same type.

You can add more than one shift register to a loop. If you have multiple operations that use previous iteration values within your loop, use multiple shift registers to store the data values from those different processes in the structure, as shown in the following figure.

Initializing Shift Registers

Initializing a shift register resets the value the shift register passes to the first iteration of the loop when the VI runs. Initialize a shift register by wiring a control or constant to the shift register terminal on the left side of the loop, as shown in the following figure.

In the previous figure, the For Loop executes five times, incrementing the value the shift register carries by one each time. After five iterations of the For Loop, the shift register passes the final value, 5, to the indicator and the VI quits. Each time you run the VI, the shift register begins with a value of 0.

If you do not initialize the shift register, the loop uses the value written to the shift register when the loop last executed or the default value for the data type if the loop has never executed.

Use an uninitialized shift register to preserve state information between subsequent executions of a VI. The following figure shows an uninitialized shift register.

In the previous figure, the For Loop executes five times, incrementing the value the shift register carries by one each time. The first time you run the VI, the shift register begins with a value of 0, which is the default value for a 32-bit integer. After five iterations of the For Loop, the shift register passes the final value, 5, to the indicator, and the VI quits. The next time you run the VI, the shift register begins with a value of 5, which was the last value from the previous execution. After five iterations of the For Loop, the shift register passes the final value, 10, to the indicator. If you run the VI again, the shift register begins with a value of 10, and so on. Uninitialized shift registers retain the value of the previous iteration until you close the VI.

Stacked Shift Registers

Stacked shift registers let you access data from previous loop iterations. Stacked shift registers remember values from multiple previous iterations and carry those values to the next iterations. To create a stacked shift register, right-click the left terminal and select Add Element from the shortcut menu.

Stacked shift registers can occur only on the left side of the loop because the right terminal transfers the data generated only from the current iteration to the next iteration, as shown in the following figure.

If you add another element to the left terminal in the previous figure, values from the last two iterations carry over to the next iteration, with the most recent iteration value stored in the top shift register. The bottom terminal stores the data passed to it from the previous iteration.

Replacing Shift Registers with Tunnels

Replace shift registers with tunnels by right-clicking the shift register and selecting Replace with Tunnels from the shortcut menu when you no longer need to transfer values from one loop iteration to the next.

If you replace an output shift register terminal with a tunnel on a For Loop, the wire to any node outside the loop breaks because the For Loop enables indexing by default. Right-click the tunnel and select Disable Indexing at Source from the shortcut menu to disable indexing and automatically correct the broken wire. If you want indexing enabled, delete the broken wire and the indicator terminal, right-click the tunnel, and select Create Indicator from the shortcut menu.

Replacing Tunnels with Shift Registers

Replace tunnels with shift registers by right-clicking the tunnel and selecting Replace with Shift Register from the shortcut menu when you want to transfer values from one loop iteration to the next. If no tunnel exists on the loop border opposite of the tunnel you right-clicked, LabVIEW automatically creates a pair of shift register terminals. If a tunnel exists on the loop border opposite of the tunnel you right-clicked, LabVIEW replaces the tunnel you right-clicked with a shift register terminal, and the cursor becomes a shift register icon. Click a tunnel on the opposite side of the loop to replace the tunnel with a shift register or click the block diagram to place the shift register on the loop border directly across from the other shift register terminal. If the shift register terminal appears behind a tunnel, the shift register is not wired.

If you convert a tunnel with indexing enabled to a shift register on a While Loop, the wire to any node outside the loop breaks because shift registers cannot auto index. Delete the broken wire, wire the output wire to the right of the shift register to another tunnel, right-click the tunnel, and select Enable Indexing from the shortcut menu, and wire the tunnel to the node.

Feedback Node

The Feedback Node, shown as follows, appears automatically in a For Loop or While Loop when you wire the output of a node or group of nodes to the input of that node or group of nodes.

You also can select the Feedback Node on the Functions palette and place it inside a For Loop or While Loop. Use the Feedback Node to avoid long wires across loops.

Like a shift register, the Feedback Node stores data when the loop completes an iteration, sends that value to the next iteration of the loop, and transfers any data type. The Feedback Node arrow indicates in which direction the data values flow along the wire. The arrow automatically changes direction if the direction of data flow changes.

If you connect the wire to the tunnel after the Feedback Node, the Feedback Node passes each value back to the input of the node and then passes the last value to the tunnel.

For example, the For Loop in the following figure iterates five times. The Feedback Node passes the value from the previous iteration to the input of the Increment function each time the loop iterates. On the last iteration of the loop, the Feedback Node passes the value from the previous iteration (4) to the input of the Increment function, which adds 1 to the value and sends the result to the tunnel. When the VI finishes running, the value in the numeric indicator is 5.

The following figure shows the same block diagram implemented with shift registers instead of a Feedback Node.

The For Loop in the following figure also iterates five times. The Feedback Node passes the value from the previous iteration of the loop to the tunnel before it passes the value to the input of the Increment function. The value in the tunnel is always the value from the previous iteration. On the last iteration of the loop, the Feedback Node holds the last value, which in this case is 5, but does not pass that value to the tunnel or to the numeric indicator. When the VI finishes running, the value in the numeric indicator is 4, which is the value from the previous, not the last, iteration of the loop.

The following figure shows the same block diagram implemented using shift registers instead of a Feedback Node.

Initializing Feedback Nodes

Right-click the Feedback Node and select Initializer Terminal from the shortcut menu to add the initializer terminal to the loop border to initialize the loop. When you select the Feedback Node on the Functions palette or if you convert an initialized shift register to a Feedback Node, the loop appears with an initializer terminal. Initializing a Feedback Node resets the initial value the Feedback Node passes to the first iteration of the loop when the VI runs. If you do not initialize the Feedback Node, the Feedback Node passes the last value written to the node or the default value for the data type if the loop has never executed. If you do not wire the input of the initializer terminal, each time the VI runs, the initial input of the Feedback Node is the last value from the previous execution.

Replacing Shift Registers with a Feedback Node

Replace a shift register with a Feedback Node by right-clicking the shift register and selecting Replace with Feedback Node from the shortcut menu. Replace a Feedback Node with shift registers by right-clicking the Feedback Node and selecting Replace with Shift Register from the shortcut menu.

Default Data in Loops

While Loops produce default data when the shift register is not initialized.

For Loops produce default data if you wire 0 to the count terminal of the For Loop or if you wire an empty array to the For Loop as an input with auto-indexing enabled. The loop does not execute, and any output tunnel with auto-indexing disabled contains the default value for the tunnel data type. Use shift registers to transfer values through a loop regardless of whether the loop executes.


Resources


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit