For Loop

Executes its subdiagram n times.

The iteration terminal provides the current loop iteration count, which ranges from 0 to n-1.

1378

Inputs/Outputs

Iteration

Current loop iteration count. The loop count always starts at zero for the first iteration.

The maximum number of iterations is 2,147,483,647, or 231-1.

If you need to keep count of more than 2,147,483,647 iterations, you can use shift registers with a greater integer range.

1378

Count

Number of times to execute the code inside the For Loop. If you wire 0 or a negative number to the count terminal, the loop does not execute.

This terminal also returns the loop count.

1378

Tunnel

Point through which data enters or exits a structure.

Tunnels can also assume the following types of functionality:

  • 1378 Keep Last Value—Passes the data through the loop border.
  • 1378 Auto Index ValuesFor a single loop iteration, either processes one element of an array if the tunnel is an input or appends a piece of data to an accumulating array if the tunnel is an output. To turn on auto-indexing for a tunnel, right-click the tunnel and select Append Mode » Auto Index Values.
  • 1378 Concatenating—Appends all elements of the input array in order, forming an output array of the same dimension as the input array. This tunnel concatenates arrays in the same way as the Build Array node.
  • You can configure a conditional output for any tunnel type by selecting the tunnel and clicking the Conditional checkbox in the Item tab. When the conditional input for a tunnel is True, the loop writes the corresponding value to the tunnel. When the condition input for the tunnel is False, the loop doesn't write the corresponding value to the tunnel.

    1378

    Shift Register

    Member of a pair of terminals that passes a value from one iteration of a loop to the next iteration. After the initial loop iteration, the left shift register in the pair returns the value it receives from the right shift register from the previous iteration.

    Refer to Accessing Data from the Previous Loop Iteration for more information about passing values from the previous iteration to the current iteration.

    1378

    Create Shift Register

    Adds a pair of shift registers to the loop to pass data from one loop iteration to the next.

    Examples

    The following image shows the output values for a tunnel and shift registers after zero loop iterations.


    1378

    Output Panel Indicator Comments
    A
    1378

    If a For Loop iterates zero times, any output tunnel of the For Loop passes the default value for that tunnel's data type.
    B
    1378

    Any output shift register passes the value wired to the input of the left shift register.
    C
    1378

    If the input shift register never contained a value, the output shift register passes the default value for that shift register's data type.

    Programming Patterns

    • Repeating Operations a Set Number of Times
    • Repeating Operations Once for Every Element in an Array

    Controlling Loop Timing

    Refer to Loop Timing for more information about controlling the execution speed of a loop.

    Accessing Data from the Previous Loop Iteration

    Refer to Accessing Data from the Previous Loop Iteration for more information about passing values from the previous iteration to the current iteration.

    Tunnel and Shift Register Behavior after Zero Iterations

    When you wire data directly through a For Loop that iterates zero times, all output terminals wired to output tunnels display the default value for the data type. All output terminals wired to output shift registers display the data wired to the input shift register. If the input shift register never contained a value, the output shift register displays the default value for that shift register's data type. Refer to the Examples section for more information.

    For Loop Iteration Count Behavior with an Auto-Indexing Input Tunnel

    By not wiring a value to the count terminal when auto-indexing is enabled, the loop automatically iterates once for each element in the array.

    Wiring a value to the count terminal while auto-indexing is enabled causes the For Loop to use the smallest of the choices between the count terminal and the input array size to determine the number of loop iterations. For example, if an auto-indexed array enters the loop with 10 elements and you wire a value of 15 to the count terminal, the loop executes 10 times.

    A For Loop can process multiple arrays one element at a time using multiple auto-indexing input tunnels. In this situation, the loop uses the smallest array size to determine the number of loop iterations. For example, if two auto-indexed arrays enter the loop with 10 and 20 elements respectively, the loop executes 10 times, processing all elements of the first array but only the first 10 elements of the second array.

    For Loop Auto-Indexing Behavior for Arrays

    When you set a tunnel to Auto Index Values, 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.

    For 2D arrays, indexing occurs at row boundaries, not column boundaries.