For Loops: Repeating Operations a Set Number of Times

A For Loop executes the section of code inside the loop, called a subdiagram, a set number of times. The count (N) terminal sets the number of times to repeat the subdiagram. One subdiagram repetition is called an iteration.

Use a For Loop any time you want an operation to occur a set number of times. For example, if you want to write a set number of measurements to a file, place the code that writes measurements to the file inside a For Loop. The numeric value you wire to the count terminal determines the number of times the writing operation occurs. After the For Loop executes the subdiagram for the number of iterations you set, the For Loop stops.

To stop a For Loop when a condition occurs,add a conditional terminal. If you add a conditional terminal, the For Loop stops when the condition occurs or when the loop finishes executing the number of iterations you set.

Passing Data through For Loops

The For Loop does not pass values from one iteration to the next by default. To access data from the previous iteration and use it in the next iteration, add shift registers to the For Loop. For example, if you want to use the result of a mathematic operation in one iteration as the initial value for a mathematic operation in the next iteration, use shift registers.

When you wire an array or a collection data type to the input tunnel of a For Loop, the For Loop enables auto-indexing by default. When auto-indexing is enabled, the For Loop reads and processes one element in the array or collection per loop iteration, rather than reading and processing the entire array or collection each iteration. You can enable or disable auto-indexing depending on how you want the For Loop to process the data.

Improving For Loop Execution Speed

If rapid For Loop execution is crucial to the operation of a VI and the machine running the VI has multiple processors, enable parallel For Loop iterations, unless enabling parallel iterations produces an error or warning. When you enable parallel iterations, processors execute iterations simultaneously to improve performance.

Related Information

For Loop

Stopping For Loops When a Condition Occurs

Shift Registers: Passing Values between Loop Iterations

Processing Individual Elements in an Array or a Collection with a Loop

Parallel Iterations: Improving For Loop Execution Speed