LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

While loops

Is there anyway to immediately break a while loop,
such as when you use "break" in a for loop, etc, in C++?

--
Slade Squire
Programmer
Rectifier Technologies Pacific
Melbourne, Australia
0 Kudos
Message 1 of 7
(3,490 Views)
A while loop will stop if control terminal is set to false, but if you mean a for loop, i think there's no way.
Good luck
0 Kudos
Message 2 of 7
(3,490 Views)
> A while loop will stop if control terminal is set to false, but if you
> mean a for loop, i think there's no way.

But even in a while-loop it wont stop immediately as Slade wants ! The
while-loop will stop as soon as the terminal is set to false AND the
source-code inside the loop is completely executed. Slade obviously wants to
stop a loop while the execution of the code inside the loop has not been
finished successfully (compare with the "break"-command in c/c++ !!!). I
dont see an easy way to do so in LabVIEW.

Good luck,

Philipp
0 Kudos
Message 3 of 7
(3,490 Views)
You can use state machines, with one state for normal execution passing trhough a state of error/break, that will depend on a error break value to inmediately exit the while loop withot executing anything else.
Hope this helps
0 Kudos
Message 4 of 7
(3,490 Views)
Usually (and I am not sure if there is any other situation) in C++ you use a "break" statement in a 'while' or 'for' loop when certain condition occur and you want the execution of the loop to end, and this condition is tested with an 'if' statement. (Some compilers will give you an error if some portion of the code can not be reached due to a 'break' statement) One way to do something similar in LabVIEW is to test the condition for the break. To avoid any other execution of the code after the condition of "break", use the "Case" structure to enclose the different parts of the code. The attached vi demonstrate this. The vi is written in LabVIEW 5. The 'while' loop 'breaks' when the user input the number 5 (or when the stop button is pressed).

In most cases, 'for'
loops can be easily substituted by 'while' loops.

Certainly, with a little of creativity, the same 'break' effect can be done in LabVIEW
www.vartortech.com
0 Kudos
Message 5 of 7
(3,490 Views)
What I would suggest is a CASE inside a WHILE loop. The Boolean for the CASE would be from your button for stopping. I've attached an example to demonstrate my point.

Hope that helps.

Shan Pin Koh
0 Kudos
Message 6 of 7
(3,490 Views)
Slade Squire wrote:
>
> Is there anyway to immediately break a while loop,
> such as when you use "break" in a for loop, etc, in C++?
Hi,
In contrast to the LV For loop which executes the wired number of iterations, the while loop exits
when the "continue?" condition becomes "false".
This construct is ideal when you do not know beforehand how many times you have to run through the loop.
--
Pierre Meyer                        
0 Kudos
Message 7 of 7
(3,490 Views)