Repeating Operations a Set Number of Times
- Updated2023-02-17
- 3 minute(s) read
Repeating Operations a Set Number of Times
Instead of creating the same code on a diagram multiple times, you can write code in a single location and use a For Loop to programmatically repeat it. For example, you might want to read a specific number of measurement samples from a device. You can place the code that performs the measurement operation on the subdiagram of a For Loop and configure the loop to repeat the operation as many times as your application requires.
What to Use
What to Do
Create the following diagram to repeat an operation a set number of times.
Customize the gray sections for your unique programming goals.

|
|
Place the code you want to repeat on the subdiagram of a For Loop. |
|
|
Specify how many times the For Loop repeats its subdiagram by wiring the desired value to the count terminal. Tip If you want the loop to execute once for each element in an array, wire the array to the loop using an auto-indexing tunnel instead of wiring a value to the count terminal.
|
|
|
If you want to know which loop iteration is currently executing, use the iteration terminal to return the current loop iteration count. The iteration terminal is zero-indexed, meaning it ranges from 0 to n -1. Use Increment to obtain the true number of loop iterations. |
|
|
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. |
Troubleshooting
- If the For Loop unexpectedly fails to execute, verify that the value wired to the count terminal is greater than zero.
- If the execution speed of the loop is too fast, place a Wait node on the subdiagram of the loop to specify a wait period.
Related Information
- Repeating Operations
When building an application, you may need to repeat code a set number of times or until a specified condition is met. For example, you may have a section of code that you know you want to run three times. You may want your code to keep running until a user clicks a stop button. You can use loops to do this.
- Repeating Operations Once for Every Element in an Array
- Repeating Operations until a Condition Occurs
- Loop Timing
Loop timing refers to how long a loop takes to execute a single iteration. The amount and type of code a loop contains affects its execution speed.