Processing Individual Elements in an Array or a Collection with a Loop

You can configure a Loop to pass individual elements of an array or a collection into and out of the loop.

Configuring the Input Tunnel to Process Individual Elements of an Array or a Collection

If you wire an array or a collection to a For Loop or While Loop, you can read and process every element in that array or collection by enabling auto-indexing. When you wire an array or collection from an upstream node to an auto-indexing input tunnel on the loop border and enable auto-indexing on the input tunnel, elements of that array or collection enter the loop one at a time, starting with the first element.

Complete the following steps to pass elements into an array through a loop:

  1. Right-click the tunnel at the loop border.
  2. Choose the option that corresponds to how you want to process array elements:
    Note Note  To avoid default data output values when a loop does not execute, enable auto-indexing every time a For Loop has an output tunnel.

A bracketed glyph appears on the loop output tunnel to indicate that auto-indexing is enabled.

Configuring the Output Tunnel to Return an Alternative Value of an Array

Complete the following steps to configure the output tunnel of the loop to return the indexed value of an array, the last value of an array, or a concatenated array:

  1. Right-click the output tunnel.
  2. Select Tunnel Mode»Last Value, Indexing, or Concatenating from the shortcut menu respectively.

Refer to the following table for more information about each of the output options the tunnel can return.

Output NameDescription
IndexingThe 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.
Last valueThe output tunnel returns only the element from the last iteration of the loop.
ConcatenatingLabVIEW appends all inputs in order, forming an output array of the same dimension as the array input wired. The Concatenating tunnel mode concatenates arrays the same as the Build Array function.

Between the output tunnel and the next node, the thickness of the wire indicates whether the loop is indexing, returning the last value, or concatenating the values. Indexing creates a thicker wire than concatenating because the wire contains an array of higher dimension instead of an array of the same dimension. When the output tunnel returns the last value, the wire is thinner than when indexing or concatenating.

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

Refer to the labview\examples\Arrays\Arrays.lvproj for examples of building arrays by disabling auto-indexing.

 Open example  Find related examples

Choosing the Type of Auto-Indexing for an Array or Collection Wired to a Loop

Loop TypeArray or Collection ConfigurationAuto-Indexing BehaviorExample
For LoopOne array or collection wired to an input tunnel and an unwired count terminalIf you enable auto-indexing on an array or a collection wired to a For Loop input tunnel, LabVIEW uses the array or collection size as the number of times to execute so you do not need to wire the count terminal. LabVIEW enables auto-indexing by default for every array or collection you wire to a For Loop.
Note Note  You can disable auto-indexing if you do not need to process arrays or collections one element at a time.
For LoopWired count terminalIf you enable auto-indexing when the count terminal is wired, the actual number of iterations that LabVIEW executes is the smaller of either the array or collection size or the value of the count terminal.For example, if you wire an array or a collection with 10 elements to a For Loop and a value of 15 to its count terminal, the loop executes only 10 times.
For LoopMultiple arrays or collectionsIf you enable auto-indexing for more than one tunnel, the number of iterations that LabVIEW executes is the smallest of the array or collection sizes.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 only 10 times, indexing all elements of the first array but 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. However, if the smaller data source includes only 50 elements, the loop executes 50 times and indexes only the first 50 elements of each data source. Use the Array Size function to determine the size of arrays.
While LoopOne wired array or collectionIf you enable auto-indexing for an array or collection entering a While Loop, the While Loop indexes the array or collection the same way as a For Loop. However, the number of iterations a While Loop executes is not limited by the size of the array or collection because the While Loop iterates until a specific condition occurs. If the number of loop iterations is greater than the array or collection size, the While Loop handles all the elements in the array or collection and uses the default value of the array or collection element type for later iterations.

You can prevent the default value from passing into the While Loop by using the Array Size or Collection Size functions, which indicate how many elements are in the array or collection. Configure the While Loop to stop executing when the number of iterations equals the array or collection size.

Auto-indexing for While Loops is disabled by default.
Tip  Because you cannot determine the size of an output array or collection in advance, enabling auto-indexing for the output of a For Loop is more efficient than that of a While Loop. Iterating too many times can cause your system to run out of memory.

Related Information

Arrays

Grouping Data with Collections

For Loops: Repeating Operations a Set Number of Times

While Loops: Repeating Operations Until a Condition Occurs

Automatically Concatenating Arrays Leaving Loops