LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

passing data from a running "for" loop

I have a for loop inside of a while loop. The iteration counter of the for loop is used to move from one item of an array to another, and performs a conditional analyis on each item. This works. However, once the condition has been met, I want to pass a "True" value to the "stop" of the while loop, stopping it and the for loop it contains, even if all the iterations of the foor loop have not been completed. I know that normally, data is not passed from the for loop until it executes all iterations, and that local variables are the potential solution. I also know that local variables use lots of memory, but that is not a concern. I am hoping for a simple solution that will not require reprogramming everything inside of the for loop, since it works great. Thank you.

0 Kudos
Message 1 of 6
(2,885 Views)
It sounds as though you should be using a while loop for the inner loop as well. If you pop-up on the bondary of the For loop, one of the menu items is "Replace with While Loop" or something similar. While loops can autoindex at input or output tunnels, but the option is disabled by default. Then you can wire the termination condition from the inner loop to the outer loop and both will stop.

Lynn
0 Kudos
Message 2 of 6
(2,878 Views)

You cannot stop a for loop early. Even if you passed a value out to the while loop, that still wouldn't stop it. That is not the way LabVIEW and dataflow programming works. all you have to do is right click on the for loop and select Replace With While Loop. Then wire your termination logic to the conditional terminal.

Also, if you are using a for or while loop, there's usually no reason to use the iteration terminal to index an array. When you wire an array into a loop, you can right click on the entry tunnel and select Enable Indexing. This is the default behavior of a for loop. With auto-indexing, you do not wire a number to the count terminal.

0 Kudos
Message 3 of 6
(2,877 Views)
Yes, but enabling indexing converts a 1d array into a 2d array, correct? If I change the for loop to a while loop, will I still be able to use an iteration counter to move from one array element to another?  I should have been specific in that every element in one array(N=256) is compared to every element in another array (N=256), so 65536 calculations are performed rather than 256. This is to calculate every possible vector formed along a discrete curve.  Using an iteration counter to keep the algorithm fixed on one element while the other array was cycled through was the most covenient method.  Will I still be able to switch from a for loop to a while loop given the above neccessities of the algorithm?  I am sincerely grateful for the input.

0 Kudos
Message 4 of 6
(2,859 Views)
When you input a 1D array into a loop, it indexes into individual elements. If you input a 2D array into a loop, it indexes it into a 1D array. You could have nested a couple of loops without indexing things manually but if you want to keep the existing code as much as possible, replace the for loop like I said. If you don't want to terminate early, then run the while loop while i is less than array size. If you want to terminate early, add this condition.
 
 
0 Kudos
Message 5 of 6
(2,850 Views)
That works.  I had no idea while loops had that kind of flexibility. Thank you for your help.
0 Kudos
Message 6 of 6
(2,843 Views)