Is there any way to exit the for each loop before the loop ends

Hi All,


Is there any way to exit the "for each" loop if I met some condition? 

something like break; on c#.net


I try to do something like this, So when the trRelatedEvents.List.Current.IsLinked = true I should be able to exit the loop right away.

But this gives me anerror "Ambiguous paths to End..."


Thanks,

Dan


Solution

Hi Dan,

Try using separate End node:

Solution

Awesome! Yes it's working.

Thanks!!

While this solves the problem, it doesn't actually answer the question?

This won't work if you have a lot of code after the loop.

You need to have the same code in both paths after the loop.

I would suggest set a variable if you have found it in the loop. Found = true. In the iteration of the loop first check this variable. If it's true go back to the start of the loop if it's false continue the iteration.

Now you have only one path after the loop and do not need to double the code after the loop.

Cheers,

Johan

Hello Nathan, 

Why do you think it does not answer the question? Because the solution given is the only way to finish a ForEach looping before it's conclusion. 

Cheers. 

Johan den Ouden wrote:

This won't work if you have a lot of code after the loop.

You need to have the same code in both paths after the loop.

I would suggest set a variable if you have found it in the loop. Found = true. In the iteration of the loop first check this variable. If it's true go back to the start of the loop if it's false continue the iteration.

Now you have only one path after the loop and do not need to double the code after the loop.

Cheers,

Johan

I agree with Johan. Although you could have a number of "wasted" cycles with this approach.

Alternatively, you could extract the foreach logic (as presented by Harlin) into a separate action and then just use that before the "lot of code after the loop".

So the answer to the question is:

NO there is no way to exit a ForEach loop early.


Does anyone know if there is an idea logged for this?
The simple addition of an option "Exit If True" parameter on the ForEach could make a huge difference.

Ignoring the fact that almost every programming language has a way to exit a ForEach loop early, and thus programmers expect to be able to do this, this must be a significant performance hit, and impact on the outsystems shared server hardware requirements.

Hi Nathan,


Yes  there are ideas for this registered there and is a wish for a long time for developers.

Cheers,

Johan

There is a very straightforward way of doing this.

Not sure if it is a good practice.

You just raise an exception on both foreach's and condition's end branch then handles it elsewhere.

It is equivalent to have multiple ways of entering the same node.

This way you can reuse your logic without replications.

Hi Edgard,

I my opinion your solution is an anti-pattern, because:

  • Exceptions are, in essence, sophisticated GOTO statements
  • Programming with exceptions, therefore, leads to more difficult to read, and understand code
  • Most languages, also OutSystems, have existing control structures designed to solve your problems without the use of exceptions

The internet has a lot of discussion on the topic of "exceptions for control flow anti pattern".

Regards,

Daniel

I like the way he thinks with that workaround, but I'd hate to use it as it would fill up the logs!

This seems to me like functionality outsystems should have added by now?

It wouldn't need a new object or anything, just allow logic where if statements allow you to route out of the loop.


I'm surprised no one at outsystems though to allow it simply from a perspective of reducing the workload on their servers...

Just to provide a quick alternative, you can create a server action, in which you exit your for-each loop early by going to an end node. 

Then pass the data back to your main flow via the output.

I do like the exception thing though, it feels a little bit like throwing out events (which would be a cleaner solution since you won't be dishing out exceptions which aren't actual exceptions).