Table Of Contents

Repeating Operations a Set Number of Times

Last Modified: November 1, 2016

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.

spd-note-tip
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 Append Mode»Auto Index Values. 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.

Examples

Search LabVIEW for the following installed lesson: For Loops


Recently Viewed Topics