Jump to content

For loop: Difference between revisions

From LabVIEW Wiki
No edit summary
Bryan (talk | contribs)
Rewritten to provide additional information on For Loops, behavior and ways of stopping executions in later LabVIEW versions.
Line 1: Line 1:
== How do I stop a for loop? ==
== Using a For Loop ==
A for loop by definition executes the requisite number of times (N). If the number of iterations is not known then you have to use a while 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.


Is there some religious prohibition against the while loop that precludes its use? There are ugly, inelegant things you could do by putting a case statement in the for loop to avoid doing anything after some number of iterations but there is no reason to do this except to introduce wasteful, inefficient, useless code. Use a while loop, you will like it.
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".


An underappreciated feature of while loops is their ability to index arrays or produce array outputs similar to for loops. To do this "enable indexing" on the wire coming into or out of the while loop. Likewise you can disable indexing on for loops to avoid the indexing feature.
== 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.


== Why doesn't my FOR loop return a value? ==
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.
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 loops always execute at least once so the outputs are always defined.
 
== 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.
 
[[Category:Structures]]


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

Revision as of 17:53, 30 January 2019

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.