Jump to content

For loop: Difference between revisions

From LabVIEW Wiki
Add tips and tricks, mark for wikification and as a stub
m Add to Structures Palette
Line 25: Line 25:
* [http://www.ni.com/white-paper/7588/en/ NI.com Tutorial on For Loops and While Loops]
* [http://www.ni.com/white-paper/7588/en/ NI.com Tutorial on For Loops and While Loops]


[[Category:Structures]]
[[Category:Structures Palette]]

Revision as of 13:25, 15 May 2020

For Loop - Will execute "your code" ten times.
For Loop With Indexed Input - Will execute "your code" ten times.
For Loop With Conditional Terminal - Will execute until first TRUE element, which would be at index 4. The remaining elements will not be indexed.

Using a For Loop

The LabVIEW 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.

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 earlier versions of LabVIEW, 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 would have to be used, in addition to having input and/or output tunnels with indexing enabled.

In later versions of LabVIEW, a conditional terminal was made available to For Loop, which functions similar to that which is used in a While Loop. 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.

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.

See Also

External Links