Jump to content

For loop: Difference between revisions

From LabVIEW Wiki
New page: == What does 0ms wait function do in a while loop == If you have multiple loops in your application that don't need to run as fast as possible, then placing a 1ms delay will limit your lo...
 
No edit summary
Line 1: Line 1:


== What does 0ms wait function do in a while loop ==
== How do I stop a for loop? ==
If you have multiple loops in your application that don't need to run as fast as possible, then placing a 1ms delay will limit your loops to at most 1000 iterations per second. Without a delay, the loop rate can be in the millions of iterations per second range depending on what is inside it. So that means that your CPU has about 1/1000th as much work to do and can go off and tend to other tasks. If your loop was already taking several ms, then the 1ms delay is likely in parallel and it won't affect the loop speed noticeably. So placing the delay in the loop can drop your CPU usage noticably and allow time for the OS to do other work like send messages that your application may be waiting for, etc.
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.


But what does a wait of 0 ms do? Let's consider the LV execution system to be a xerox copying machine. Everyone that has something to copy heads for the copying machine and lines up. If you have never had the pleasure of waiting at a copy machine, then consider any other time consuming task where people wait in line. Everytime a LV diagram executes a wait function, it is like releasing the copier and staying out of the line for some time delay. After the delay has passed, you will get back in line to make your next copy. A wait of 0 ms is like immediately going to the end of the line to let a smaller copy-task take place. If nobody is in line behind you, you immediately start your next copy task. If someone is in line, it lets them take a turn.
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.


This 0 ms wait is a pretty cool little trick to make LabVIEW loop parallelism less chunky. It naturally adds some overhead because the loops have some setup time, but when the loops are doing significant work it is tiny. Use it whenever you think you need to, but beware that if some loops don't have any delay, they are still going to  
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.


hog the CPU, and the wait of 0 ms may turn into much larger waits because the loops with no waits play by different rules.
== 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 loops always execute at least once so the outputs are always defined.
== Is the while loop really a while loop? ==
No. Strictly speaking the while loop is a "do while" (in C) or repeat until (in PASCAL) programming structure. In other words the loop always executes at least once, and the Boolean test for continuation is done at the end of the loop execution. With a true while loop in a text based program the test is done prior to executing any of the commands contained within the loop.
 
To create a "real" while loop wire the output of your terminating condition to a case structure surrounding the rest of the code within the loop.
 
This is of course true, but then there is no real difference between a while loop as you describe it and a for loop in C either. Of course the syntax changes but the functionality is really equivalent.
And instead of putting the case structure inside the while loop you could just as easily put the while loop in the case structure and get the same behaviour and IMO it looks more clear what is happening.
 
Also true, but then you loose having a shift register from the loop hold variable(s) that are part of the exit test, unless you then wrap the whole thing in another loop.
 
[QUOTE]
and the Boolean test for continuation is done at the end of the loop execution.[/quote]
 
This is a little misleading, particularly for new [[LabVIEW]] programmers. The boolean test for continuation is done as soon as possible... it only waits until the end of loop execution if there is a data dependency. With no dependacy, the loop will decide whether to execute again before/in parallel with any other code in the loop.
 
I have seen plenty of people getting one extra iteration than they expected because of this.

Revision as of 05:10, 18 March 2007

How do I stop 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.

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.

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.

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 loops always execute at least once so the outputs are always defined.