Jump to content

For loop: Difference between revisions

From LabVIEW Wiki
m Add to Structures Palette
m Logmanoriginal moved page For Loop structure to For loop: Only the first word in a title should start with a capital letter
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{wikify}} {{stub}}
{{TOCright}}
{{LabVIEW Palette Object Information|palette=Functions Palette/Programming/Structures{{!}}Structures palette|type=structure|icon=Functions Palette - Programming - Structures - For Loop.png}}
 
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.
 
[[File:ForLoop.PNG|thumb|For Loop - Will execute "your code" ten times.]]
[[File:ForLoop.PNG|thumb|For Loop - Will execute "your code" ten times.]]
[[File:ForLoop Indexing.png|thumb|For Loop With Indexed Input - Will execute "your code" ten times.]]
[[File:ForLoop Indexing.png|thumb|For Loop With Indexed Input - Will execute "your code" ten times.]]
[[File:ForLoop Indexing Conditional.png|thumb|For Loop With Conditional Terminal - Will execute until first TRUE element, which would be at index 4.  The remaining elements will not be indexed.]]
[[File:ForLoop Indexing Conditional.png|thumb|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.


== 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".
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? ==
=== 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 [[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 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.
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.


== Why doesn't my For Loop return a value? ==
== 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.
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 ==
== Tips and tricks ==
* Wire an [[error cluster]] to the conditional terminal to terminate the loop on an error.
* Wire an [[error cluster]] to the conditional terminal to terminate the loop on an error.
== History ==
{| class="wikitable"
! Version
! Change(s)
|-
|[[File:LV2020.png|frameless|border|64x64px|LabVIEW 2020|link=LabVIEW 2020]]
| In [[LabVIEW 2020]] the Iteration Terminal visibility can be toggled.
|-
|[[File:LV8-2013.png|frameless|border|64x64px|LabVIEW 8.5|link=LabVIEW 8.5]]
| In [[LabVIEW 8.5]] the Conditional Terminal was added to allow For Loops to end early.
|}


== See Also ==
== See Also ==

Latest revision as of 15:00, 7 May 2023

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.

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.

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)
LabVIEW 2020 In LabVIEW 2020 the Iteration Terminal visibility can be toggled.
LabVIEW 8.5 In LabVIEW 8.5 the Conditional Terminal was added to allow For Loops to end early.

See Also

External Links