LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I implement a "goto" in exiting loops?

Hi, I've encountered a problem when trying implement an early exit from a loop. Can anyone advice me on this, really wish to how to tackle this issue.
 
 
Thanks in advance...
0 Kudos
Message 1 of 6
(3,002 Views)
I'm going to assume you're using a For loop...  If this is the case you can do two things.  One, you could switch to a while loop, and just have the conditions 'if i = [former input to For loop N], stop', as well as the early exit condition wired to the stop (ORed).

The other is that you could have all of your code inside error cases (case structure with error wired to the case selector), and once your early exit condition occurs, set the error to true, and the For loop will finish itself without executing any more code (essentially instant).

I would recommend the while loop method, as there's less wasted CPU cycles.  I could however be way off with this answer.  If so, please be a bit more specific. 

Hope this helps


Message 2 of 6
(2,999 Views)
Good suggestion but in this case I would reccommend a statemachine type of architecture. simply because it is more upgradeable and flexible.



Joe.
"NOTHING IS EVER EASY"
Message 3 of 6
(2,993 Views)
Joe is right, pardon my omission.  State-machine architecture is sort've a mish-mash of the two suggestions I provided, and is far more robust than either solution.

If you need help with that, let us know!
0 Kudos
Message 4 of 6
(2,986 Views)
This is a frequently asked question. While the state machine is a good design paradigm, I think based on the original post the coder isn't really looking for a state machine construct. He just wants to break out of the loop early. As the FAQ indicates, you can't unless you do something like the case structure business mentioned earlier, or you use a while loop.
0 Kudos
Message 5 of 6
(2,976 Views)
Again, all suggestions will suit his/her needs. The only reason I suggested the statemachine is because it is a great paradigm to use even in simple matters as this one. and it will allow him/her to upgrade the vi and add to it as he/she sees fit. I also agree that a statemachine just to do this is probably overkill it still adds flexablity to the code.



Joe.
"NOTHING IS EVER EASY"
0 Kudos
Message 6 of 6
(2,963 Views)