How to continue or exit a loop (while)?

loop

#1

There are something similar to “Exit While” and “Continue While” of vb.net?

Exemple:

> Dim index As Integer = 0
> While  index < 100000
>     index += 1
>
>     If index >= 5 And index <= 8 Then
>         Continue While
>     End If
> 
>     If index = 10 Then
>         Exit While
>     End If
> End While

#2

I don’t think Uipath break works in while or do while.

you could do something like (bad coding)

  1. Dim index As Integer = 0

While index < 100000
index += 1
If index = 10 Then
Exit While – index = 100000
End If
End While

  1. Dim index As Integer = 0

While index < 100000
index += 1
If index = 10 Then
tempindex = index
** index = 100000**
End If
End While
index = tempindex


#3

Youre correct, break works only in the reimplemented foreach. Rest is stock MS activities.


#4

The newest version of UiPath has an “Invoke Code” activity that will allow you to execute arbitrary VB code:

You can also “program” any kind of branching behaviour in Flowcharts, using Decision stages for the main loop condition and any internal branches.

Here are two UiPath examples of your loop code, modified with a Write Line to make it do something :wink::


#5

Hey,

I have been doing this using a Try/Catch.

I nest the Do-While in the Try/Catch, then in the If condition, use Throw Exception. Then, it exits to the Catch and out of the loop.

Thanks.


#6

You can indeed take this approach, but there’s a big if: if anything inside the loop can also throw an exception and you’re catching the Exception type to break from the loop, these other exceptions will be caught, not handled and lost. This makes things very hard to debug if you forget about this and modify the loop later, especially if the loop is complex and invokes other workflows. So, proceed with caution and be sure to document (with a comment or annotation) where you use it for yourself or anyone else working on this workflow.


#7

Good point @sfranzen

You could use Rethrow when debugging which is needed when you use Try/Catch to skip over parts when an Error is thrown and you don’t always recognize actual errors.

You can also use specific Exception names to catch the errors or even Throw a custom Exception and use that as your Exception name. It will catch the Exception in order of most specific I believe.

So, those are things I can add to that.

And, glad you pointed that out. Thanks.


#8

As @sfranzen suggests, [Invoke Code] activity works beautifully only if the whole loop commands are coded inside the [Invoke Code] activity.

However, there must be loads of circumstances to use [For Each Row] activity in order to utilize various activities inside the loop while “Continue For” statement is absolutely necessary.

I think we should definitely have an activity called something like [Continue For] to make this possible.

What do you think, guys?


#9

Yes i do agree with @hojoong.kim


#10

this is the simplest way with existing activities


#11

can you give me sample WF


#12

Main.xaml (17.1 KB)

Here it is.
You can connect to the throw activity as many times as you need the Exit/Continue condition to be executed.


#13

Thank @akhi_s27 for sharing the skeleton of the try/catch idea.
I think this should only be used as a workaround to mimic the traditional Continue/Next concept until we can see the following from the Activities panel.
(And my Designer panel looks ugly and messy with this… LOL)

Cheers!