LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement break from for loop as in c programming language

Hi,
    I want to stop "for loop" for certain condition, so is there any way to implement the break statement to "for loop" as in C programming language.
 
Regards
Shivakumar Singh
0 Kudos
Message 1 of 4
(3,403 Views)

You can't.
You can either use the Abort VI (from the Application Control palette) which will stop the entire VI, and is bad practice anyway, or you can replace your for loop with a while loop and add your condition to the stop condition using an Or node.


___________________
Try to take over the world!
0 Kudos
Message 2 of 4
(3,392 Views)

One other possibility, depending on how many times the loop is set to execute, is to put a case statement around the code you wish to no longer execute. You should have some "wait" function in your loop to keep it from taking all of the CPU when looping, just make the one in the "non-execute" case very short (i.e. 1mS). Of course if the loop is expected to execute 1000's of times in the normal case you will get the obvious delay from whatever iteration it is on until it completes. The best answer, as stated before, is to restructure the code to allow a while loop. It can be made to essentially perform like a For Loop, with a count value comparison to the interation value, but it has the additional advantage of being able to terminate at any time. If there are instances where you don't want the internal code to execute even once you will still need to put it into a case statement as the while loop will execute at least once, even if the "stop loop" is set to terminate.

 

P.M.

Putnam
Certified LabVIEW Developer

Senior Test Engineer North Shore Technology, Inc.
Currently using LV 2012-LabVIEW 2018, RT8.5


LabVIEW Champion



0 Kudos
Message 3 of 4
(3,383 Views)
Although there is no "Break" and Continue statement like in c, Labview provides great control over the Boolean logic to control the loop termination, just or the conditions you wish to "break" on to determine if the loop should continue such as error conditions, timeouts or invalid parameter assertions.  LV_Pro suggest using conditional execution with a case statement inside the loop which is a great way of controlling execution inside the loop.  You can also use uninitiallized shift registers to continue a loop where it was terminated.  Even though this is a different control from the logic of c with its break and continue statements, most c programs use Boolean logic at the beginning of a loop to determine if the loop should terminate.  I like that LV omitted a break statement in the G language specifications, but I too had a slight problem getting use to the paradigm.  REMEMBER LV is a dataflow model of programming so the data should determine the logic and breaks dont seem to fit well with this idea.  But dont worry there always seems to be work-arrounds in LV.  Hope thsi helps
Paul
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 4 of 4
(3,370 Views)