While working with an array of data, you might want to access individual elements within the array. Although you can use a combination of Array nodes to accomplish this task, the For Loop includes an auto-indexing tunnel that you can use to access the individual elements with minimal additional code.
Use a For Loop with an auto-indexing input tunnel to process one element of an array during each iteration of the loop. A For Loop with an auto-indexing input tunnel behaves similarly to a for each loop in other programming languages.
For Loop with an auto-indexing input tunnel
Create the following diagram to repeat an operation once for every element in an array.
Customize the gray sections for your unique programming goals.
|Place the code you want to repeat on the subdiagram of a For Loop.|
| When you wire an array to a For Loop, the For Loop processes one element of the array at a time. This configuration, known as auto-indexing, occurs by default. The input tunnel appears as a white box with brackets to indicate that it is an auto-indexing tunnel.
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.
|You can access each individual element of the input array by wiring the auto-indexing tunnel to the code on the subdiagram of the loop. The wire entering the auto-indexing tunnel carries 1D array data, whereas the wire leaving the auto-indexing tunnel carries scalar data.
For multidimensional input arrays, the wire leaving the auto-indexing tunnel carries array data that is one dimension smaller than the input array. For example, if the input array terminal passes a 2D array to the auto-indexing tunnel, the wire leaving the auto-indexing tunnel carries 1D array data.
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.
|If you want to collect the result of each loop iteration in an array, use an auto-indexing output tunnel to pass values out of the loop. To enable auto-indexing for an output tunnel, right-click the tunnel and select . If you do not enable auto-indexing, the loop returns only the value from the last loop iteration.|
Search within the programming environment to access the following lesson: For Loops