For Loop
- Updated2023-02-17
- 5 minute(s) read
For Loop
Executes its subdiagram n times.
The iteration terminal provides the current loop iteration count, which ranges from 0 to n-1.

Inputs/Outputs
Iteration
Current loop iteration count. The loop count always starts at zero for the first iteration.
If you need to keep count of more than 2,147,483,647 iterations, you can use shift registers with a greater integer range.

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.

Tunnel
Point through which data enters or exits a structure.
Tunnels can also assume the following types of functionality:



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.

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.

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.

Output | Panel Indicator | Comments |
---|---|---|
A |
![]() |
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 |
![]() |
Any output shift register passes the value wired to the input of the left shift register. |
C |
![]() |
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.