# How to continue or exit a loop (while)?

**URL:** <https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726>\
**Category:** Help\
**Tags:** system, activities\
**Created:** [July 26, 2017, 6:38pm UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726 "2017-07-26T18:38:13Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabios8](https://avatars.discourse-cdn.com/v4/letter/f/a87d85/32.png) [@fabios8](https://forum.uipath.com/u/fabios8)\
**Post date:** [July 26, 2017, 6:38pm UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/1 "2017-07-26T18:38:13Z")

</div>

There are something similar to “Exit While” and “Continue While” of [vb.net](http://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

```

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [July 26, 2017, 6:56pm UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/2 "2017-07-26T18:56:44Z")

</div>

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**

---

<div class="post-metadata">

**Author:** ![andrzej.kniola](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/andrzej.kniola/32/8835_2.png) [@andrzej.kniola](https://forum.uipath.com/u/andrzej.kniola)\
**Post date:** [July 26, 2017, 7:39pm UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/3 "2017-07-26T19:39:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sfranzen](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@sfranzen](https://forum.uipath.com/u/sfranzen)\
**Post date:** [July 26, 2017, 8:26pm UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/4 "2017-07-26T20:26:56Z")

</div>

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

> [@Invoke Code only in Community Edition](https://forum.uipath.com/t/invoke-code-only-in-community-edition/6277):
>
> How many here realize that the community edition allows programming directly in the flow diagram? To me, a script is essential in automation. Yet it is not available in the enterprise trial. Hopefully Invoke Code will soon be available in both versions. But every day it is not, time is spent with building cluttered workflows that are difficult to maintain.

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 😉:

[[Album] Branching while loop examples ![](https://global.discourse-cdn.com/uipath/original/3X/0/1/01a32de85e0e376e516282637898fcdd815ea7c2.jpeg "Branching while loop examples") ](https://imgur.com/a/OBugO)

---

<div class="post-metadata">

**Author:** ![ClaytonM](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/claytonm/32/555957_2.png) [@ClaytonM](https://forum.uipath.com/u/ClaytonM)\
**Post date:** [July 26, 2017, 9:51pm UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/5 "2017-07-26T21:51:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sfranzen](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@sfranzen](https://forum.uipath.com/u/sfranzen)\
**Post date:** [July 27, 2017, 10:37pm UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/6 "2017-07-27T22:37:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ClaytonM](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/claytonm/32/555957_2.png) [@ClaytonM](https://forum.uipath.com/u/ClaytonM)\
**Post date:** [July 27, 2017, 10:51pm UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/7 "2017-07-27T22:51:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hojoong.kim](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/hojoong.kim/32/7106_2.png) [@hojoong.kim](https://forum.uipath.com/u/hojoong.kim)\
**Post date:** [October 25, 2017, 6:03am UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/8 "2017-10-25T06:03:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rkelchuri](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rkelchuri](https://forum.uipath.com/u/rkelchuri)\
**Post date:** [October 26, 2017, 5:51am UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/9 "2017-10-26T05:51:26Z")

</div>

Yes i do agree with @hojoong.kim

---

<div class="post-metadata">

**Author:** ![akhi\_s27](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/akhi_s27/32/1687_2.png) [@akhi\_s27](https://forum.uipath.com/u/akhi_s27)\
**Post date:** [October 26, 2017, 6:17am UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/10 "2017-10-26T06:17:31Z")

</div>

this is the simplest way with existing activities

---

<div class="post-metadata">

**Author:** ![rkelchuri](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rkelchuri](https://forum.uipath.com/u/rkelchuri)\
**Post date:** [October 26, 2017, 6:29am UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/11 "2017-10-26T06:29:45Z")

</div>

can you give me sample WF

---

<div class="post-metadata">

**Author:** ![akhi\_s27](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/akhi_s27/32/1687_2.png) [@akhi\_s27](https://forum.uipath.com/u/akhi_s27)\
**Post date:** [October 26, 2017, 7:41am UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/12 "2017-10-26T07:41:04Z")

</div>

[Main.xaml](https://global.discourse-cdn.com/uipath/original/2X/e/ee56a5f66ded8a55e644333351944383910570bd.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.

---

<div class="post-metadata">

**Author:** ![hojoong.kim](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/hojoong.kim/32/7106_2.png) [@hojoong.kim](https://forum.uipath.com/u/hojoong.kim)\
**Post date:** [October 26, 2017, 8:49am UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/13 "2017-10-26T08:49:30Z")

</div>

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)

> [@hojoong.kim](#):
>
> [Continue For]

Cheers!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [October 29, 2018, 9:24am UTC](https://forum.uipath.com/t/how-to-continue-or-exit-a-loop-while/8726/14 "2018-10-29T09:24:47Z")

</div>

Closed on October 29, 2018, 9:24am UTC


