Exit loop when condition is met

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.

I use the BREAK for this. This should Work.

Find it under WorkFlow / Control Flow / Break

2 Likes

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

2 Likes

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.

2 Likes

BTW Break should also Work on the Do While loop

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

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.

2 Likes

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.

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.

3 Likes

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.

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.

So what is the solution to break from a while loop activity?

Thanks for support. I am trying to unable to get out put. Please support with sequence in stufio file

thanks