LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to jump out of a FOR loop?

Hi,
Please look at the attached VI file. I want to stop the FOR loop after I click the 'stop' button, how can I do that?
In my program, I need this for loop and I can't use a while loop to replace it.
Thanks.
0 Kudos
Message 1 of 4
(2,911 Views)
You can't stop a for loop. It will always run to completion. The best you can do is put a case statement inside so that one case does nothing. Imho, this is a bit of of kludge. You can, however, always replace a for loop with a while loop. Why do you think you can't do this?
Message 2 of 4
(2,907 Views)
Here is the same vi with a while loop instead of a for loop. The stop button will break out of the loop.
- tbob

Inventor of the WORM Global
0 Kudos
Message 3 of 4
(2,892 Views)
A FOR loop is only appropriate if you need to execute something a fixed number of times and that number is known at the start of the loop.
In all other cases you should use a WHILE loop, it will run until certain conditions (e.g. pressing a stop button ;)) are met.

Of course in this particular simple case, you could cheat and stop the FOR loop by stopping the entire VI (See image), but that's NOT the way to go, of course (There is typically other code that needs to keep running!). You should use something along the lines suggested by tbob.
0 Kudos
Message 4 of 4
(2,877 Views)