Exit loop when condition is met

i_declined

#1

I need the opportunity to exit loop as soon as my condition is met. It does not make sense, that I need to fully execute my entire body in the ‘While’ loop before I can exit.

The only way I can possibly imagine this happening, is by putting my loop inside a try catch and then a throw to exit, but this is not best practice.


Break a cycle do while
How to break a While loop/Do while loop
#2

I use the BREAK for this. This should Work.

Find it under WorkFlow / Control Flow / Break


#3

As far as I can see and understand, the Break activity is limited to the ‘For Each’ loop which limits its use.


#4

Then Code a " OR Finished = TRUE" on the case you use to do the controlled loop on.

And make a variable Finished as BOOLEAN = FALSE and se it TRUE when you are ready to stop.


#5

BTW Break should also Work on the Do While loop


#6

Indeed, Break is available in For Each loops only. As for While you can use the above solution (change the condition)


#7

I just tested Break on While and Do While. I get the error description: “Activity can only be placed inside a ForEach”.

In regards to your other solution, the boolean condition would not help me as the condition statement is only considered on each new loop. Thus, when my condition boolean is changed midway through the loop, I still need to finish the remainder of my code inside the body, before the loop acknowledges that I expect it to finish.

What I need is what the Break activity provides for the ForEach. A possibility to exit the loop AS SOON as my condition is met.


#8

Than implemented like this (to break in the middle):

While (c1 and not finished)

{

  actions 1

  if (not finished)

     than action set 2

}

We can not change how while behaves as that is an activity provided by Microsoft.


#9

Thanks for both of your replies.

I agree that this solution that you presented, Mihai, makes sense for very simple loops. However, it will make for terrible reading/visual representation for more complex processes, where I would then probably need multiple nested if statements.

It is unfortunate that you are unable to change and are limited by Microsoft but from a pure programming point of view the current activity cannot be described as a fully functional While loop.


#10

While I agree that having an actual break would be great, in my experience after you pass the point where nesting/chaining if’s makes sense, it’s better to redesign as a flowchart or even a state machine (depending on transition complexity and other needs).

Both of them have higher memory footprint, but are much more readable in the long run for these kind of situations.


#11

While I agree to a certain extent, the entire point of the while loop is to continuously loop through my body AS LONG as my condition is valid. AS SOON as my condition is invalid I need to exit the loop. Without this possibility it makes it more of a While’ish loop more than an actual While loop.

I want to thank both of you for your solutions and thoughts though. As long as Microsoft limits this functionality work-arounds are needed.