Showing results for 
Search instead for 
Did you mean: 

Count specific while iteration

Go to solution

Greetings ladies and gentleman;


So, i have the following issue.

I want to know the exact amount of iterations a while loop is executed. The main problem is that, this while loop, is not always running, and sometimes it jumps to the next step of the program and the comes back to the same while-loop.

In the attached VI you can see a variable named Y, colored RED that is, in fact, counting this particular while loop. The thing is that, when it meets the condition and moves forward in the program, then it has a possibility to return to this particular loop, but when it returns i lose my account iterations and starts all over again.


The program is simple, it just takes some random numbers and if it meets the conditions of > or < then it chooses the next path. The Leds are there to know in wich while-loop it is runing. I have changed the wait in the while-loop i would like to measure so it can repeat itseld a lot if times.


Thanks, if i dont explain it too well, plis let me know.


Kind Regards,





0 Kudos
Message 1 of 8

Use an unitialized shift register on the loop. It will start at zero and you will have to do your own incrementing. However, the next time the loop is called, it will retain the previous value.


This program is way more complex than it has to be. Get rid of those sequence structures and spend some time with the LabVIEW tutorials and this will get much cleaner.


0 Kudos
Message 2 of 8

You definately need a shift register on that loop.  I would also recommend another shift register on the outter loop.  This way you can keep track of the number of iterations and possibly reset it if you so desire.

There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 8



I would guess that you have programmed in some text-based language and are now learning LabVIEW, right?  The structure of your code looks like a conversion from a text-based language and not one which takes advantages of LabVIEW's dataflow paradigm.


Try to do this without duplicated code (all of the case structures and several while loops) and without sequence structures.  For the duplicated code create subVIs. The sequence structures can ususllay be eliminated by dataflow.  Your stacked sequence structures might be replaced by for loops.


You may also want to consider a better way to stop the program. As it presently is constructed the Stopp button is read almost immediately when an iteration of the outer while loop begins. The code inside the sequence structure takes several seconds or longer to execute. Assuming the Stopp button was pressed shortly after (say 100 ms) the iteration started, the loop will not stop until that iteration completes AND the next iteration (when the True Stopp value is read) completes. This makes the user interface seem very unresponsive to the user.  probably something like a state machine architecture could reduce the code to one loop which would respond to the user in ~100 ms.


To do the count you want Y needs to be on a shift register on the outer loop. After the inner loop where Y now is located completes, add the value of i for that loop to the value in the shift register.



0 Kudos
Message 4 of 8

Thanks all for your kind advices.


I, in fact, am still new to this LabVIEW structure. I have readed the tutorials but still is a little bit complicated for me.

I will check the shift register to solve my issue, thanks on that.


And for the other stuff, i will also check them and try to, as you guys said, make it cleaner. I know this is not the only way this can be done, but this VI is part of a bigger program (of course) and still i am learning to use state machine or subVI. Anyway, if you guys have a link for these couple of things, i would be very greatfull, since i am in the part of the proyect where i can reduce this whole big program.


Thanks again, will check shift register and share kudos.




0 Kudos
Message 5 of 8

The loop counter will always reset once you leave and return to the while loop. Why not make y = y + i and initialise y to zero at the very start before you enter the main loop.

By the way, in general, stacked sequence structures make code difficult to read.

0 Kudos
Message 6 of 8

Try this. It's just a simple program to demonstrate how to keep track of the loop iterations over several loop calls.

0 Kudos
Message 7 of 8
Accepted by Stgo

Here is the way I would modify your VI to just add the cumulative count (and not the other changes I suggested). I also put in the Y array indicator so you would have a history of the individual Y counts. I shortened the Waits to 50 ms and the threshold to 99 to save testing time.


Edit: I did make one other change: The logic in the second frame of the flat sequence had many redundancies. The output is equal to the >50 comparison at all tiems so I eliminated the extra code.



Message 8 of 8