For loop
Object information | |
---|---|
Owning palette(s) | Structures palette |
Type | Structure |
Requires | Basic Development Environment |
Icon |
The For Loop is a structure that will execute code contained within a finite number of times. The number of times that the loop will execute is initially determined by the numeric value written to the "N" terminal of the loop, or by the size of an array wired as an input to the loop (when indexing is enabled for the input tunnel). The latter method of specifying the number of iterations is similar to the For-Each structure in many text-based programming languages.
Usage
Outputs from a For Loop will be an array by default unless alternate behavior for the tunnel is selected by right-clicking on the tunnel and selecting a behavior under "Tunnel Mode".
How do I stop a For Loop?
In LabVIEW 8.20 and earlier, there was no way to stop the execution of a For Loop until the requisite number of iterations have been met. In those earlier LabVIEW versions, a While Loop structure would have to be used, in addition to having input and/or output tunnels with indexing enabled.
In LabVIEW 8.5, a conditional terminal was made available to a For Loop structure, which functions similar to that which is used in a While Loop structure. The conditional terminal can be made visible by right-clicking on the border of a For Loop and selecting "Conditional Terminal". This conditional terminal is useful if a condition has been met that would make further iterations of the loop unnecessary. A simple example of this could be if indexing a 10 element array in search of a particular value, then finding the desired value at the fourth element. The loop could then be stopped without having to continue to iterate through the remaining elements.
Best practice
Why doesn't my For Loop return a value?
As a good programming rule in LabVIEW, NEVER output a value from a For Loop with indexing disabled. When the loop does not execute (0 iteration) a non indexing output stays undefined and will hold any garbage left there by previous memory usage. This is because the wire output from the For Loop has no code or data source to get the value from when the loop does not execute. Instead, use a shift register (SR) to output the value and at the left SR enter a default value for the case when the loop does not execute. When the For Loop does not execute this default value of the left SR is passed to the right SR. Similarly passing a Refnum through a For Loop that never executes (0 iterations) destroys the reference. Either pass the reference using a SR as described above or wire around the loop. Note that While Loop always execute at least once so the outputs are always defined.
Tips and tricks
- Wire an error cluster to the conditional terminal to terminate the loop on an error.
History
Version | Change(s) |
---|---|
In LabVIEW 2020 the Iteration Terminal visibility can be toggled. | |
In LabVIEW 8.5 the Conditional Terminal was added to allow For Loops to end early. |