# How to break a While loop/Do while loop

**URL:** <https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442>\
**Category:** Help\
**Tags:** studio, system, activities\
**Created:** [October 10, 2017, 1:20pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442 "2017-10-10T13:20:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![yogeshgyw](https://avatars.discourse-cdn.com/v4/letter/y/94ad74/32.png) [@yogeshgyw](https://forum.uipath.com/u/yogeshgyw)\
**Post date:** [October 10, 2017, 1:20pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/1 "2017-10-10T13:20:08Z")

</div>

How to break a While loop/Do while loop. Break activity is not working, seems it will work only for for each.

---

<div class="post-metadata">

**Author:** ![andr3y26](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/andr3y26/32/7783_2.png) [@andr3y26](https://forum.uipath.com/u/andr3y26)\
**Post date:** [October 10, 2017, 1:28pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/2 "2017-10-10T13:28:16Z")

</div>

Hello,

Maybe this will help,

> [@Exit loop when condition is met](https://forum.uipath.com/t/exit-loop-when-condition-is-met/121):
>
> 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.

Regards

---

<div class="post-metadata">

**Author:** ![Lucas.Pimenta](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/lucas.pimenta/32/170092_2.png) [@Lucas.Pimenta](https://forum.uipath.com/u/Lucas.Pimenta)\
**Post date:** [October 10, 2017, 1:29pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/3 "2017-10-10T13:29:38Z")

</div>

Hello!

A While Loop/Do Loop, must have a condition to met. Try to do a If when you need to exit the loop which will give the condition to exit the loop 🙂

Regards,

---

<div class="post-metadata">

**Author:** ![Raghavendraprasad](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/raghavendraprasad/32/66936_2.png) [@Raghavendraprasad](https://forum.uipath.com/u/Raghavendraprasad)\
**Post date:** [August 13, 2018, 9:11am UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/4 "2018-08-13T09:11:27Z")

</div>

I am clueless as to why the Try Catch method would not be considered as good practice.

Essentially you catch an exception (maybe element doesn’t exist, row end, pagination end etc etc) and update a value that does not satisfy the Do WHile/WHile VB Expression, eventually ending it. (An exception is basically developer defined and there is no hard and fast rule in that regard)

There have been requests for ‘Continue’ options and break options for While (not so relevant), but a workaround that helps you achieve the objective without shortcomings can also be considered.

Tell me what you think.

---

<div class="post-metadata">

**Author:** ![selrac](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/selrac/32/468_2.png) [@selrac](https://forum.uipath.com/u/selrac)\
**Post date:** [April 10, 2019, 9:49am UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/5 "2019-04-10T09:49:33Z")

</div>

What about forcing to meet the condition.

For example if the condition is x \< 10, then you can assign x = 10 when you want to end the loop

---

<div class="post-metadata">

**Author:** ![selrac](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/selrac/32/468_2.png) [@selrac](https://forum.uipath.com/u/selrac)\
**Post date:** [April 10, 2019, 9:58am UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/6 "2019-04-10T09:58:06Z")

</div>

I think that the TC (TryCatch) will work in some cases as a solution

The problem is that if an error is found in a step inside the loop you might want a different behavior rather that continue after the loop. For example, we have TC that restarts the process if an error is found to prevent to stop production.

---

<div class="post-metadata">

**Author:** ![Jay\_Lord\_Abueva](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/jay_lord_abueva/32/39009_2.png) [@Jay\_Lord\_Abueva](https://forum.uipath.com/u/Jay_Lord_Abueva)\
**Post date:** [September 2, 2019, 3:26am UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/7 "2019-09-02T03:26:03Z")

</div>

If you want to, you can use alternatively for loop. But for me, it would be more effective if you put do while inside a try catch and if you want to stop the respective loop, you can just throw an error.

---

<div class="post-metadata">

**Author:** ![Kristopher](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/kristopher/32/106943_2.png) [@Kristopher](https://forum.uipath.com/u/Kristopher)\
**Post date:** [November 14, 2019, 4:55pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/8 "2019-11-14T16:55:40Z")

</div>

> [@Break or Continue for while loops](https://forum.uipath.com/t/break-or-continue-for-while-loops/103110/8):
>
> Try this custom activity [https://go.uipath.com/component/while-loops-with-break-and-continue-activities](https://go.uipath.com/component/while-loops-with-break-and-continue-activities)

---

<div class="post-metadata">

**Author:** ![bakerjan](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/bakerjan/32/176658_2.png) [@bakerjan](https://forum.uipath.com/u/bakerjan)\
**Post date:** [June 3, 2020, 6:10pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/9 "2020-06-03T18:10:25Z")

</div>

Hello,

Do you have examples using While Break. I have a project that I am iterating though each row on my application page, but the number of lines changes for every invoice. I used a Do while with a condition of True, but of course it becomes an infinite loop. I tried the try catch option, but that did not work either.

 ![image](https://global.discourse-cdn.com/uipath/original/3X/5/6/56d7e256b684ceb7780e38dff420375df3383d88.png) ![image](https://global.discourse-cdn.com/uipath/original/3X/3/3/3351d33ef14c1d323c3a16b2eef8e2d0ebad19cb.png) Thank you

---

<div class="post-metadata">

**Author:** ![Kristopher](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/kristopher/32/106943_2.png) [@Kristopher](https://forum.uipath.com/u/Kristopher)\
**Post date:** [June 3, 2020, 6:25pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/10 "2020-06-03T18:25:37Z")

</div>

Hi @bakerjan,

Your particular scenario sounds like a For Each Loop would be a better choice.  
If your invoices are in a spreadsheet, you could probably read the data into a DataTable and use the For Each Row activity to iterate through the data that way.

Thanks

---

<div class="post-metadata">

**Author:** ![bakerjan](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/bakerjan/32/176658_2.png) [@bakerjan](https://forum.uipath.com/u/bakerjan)\
**Post date:** [June 3, 2020, 6:50pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/11 "2020-06-03T18:50:43Z")

</div>

Thanks for the quick response. Currently my query does not show how many lines. I can add the lines, but can’t figure out how to make it work.

![image](https://global.discourse-cdn.com/uipath/original/3X/c/4/c4b8c74600d6f9923c807b2c64785ff6a4f960f3.png)  
Voucher/AP BU Invoice Number TAX CODE Lines  
10101 9010058148 CA00 1  
10101 9010057425 CA00 1  
10101 9010057425 CA00 2  
10101 9010057425 CA00 3  
10101 9010057425 CA00 4  
10101 9010057425 CA00 5  
10101 971746196 CA00 1  
10101 971746196 CA00 2

![image](https://global.discourse-cdn.com/uipath/original/3X/a/c/ac848d86380b5bd9af4c468fe41e5f744088210f.png)

 ![image](https://global.discourse-cdn.com/uipath/original/3X/7/9/79ccd58e50aa4f1dcfa2c0601ee63d96f2b3bbc5.png)

---

<div class="post-metadata">

**Author:** ![Kristopher](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/kristopher/32/106943_2.png) [@Kristopher](https://forum.uipath.com/u/Kristopher)\
**Post date:** [June 3, 2020, 7:10pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/12 "2020-06-03T19:10:40Z")

</div>

Where is your Voucher/AP BU table coming from?  
How are you storing that data in memory?

---

<div class="post-metadata">

**Author:** ![bakerjan](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/bakerjan/32/176658_2.png) [@bakerjan](https://forum.uipath.com/u/bakerjan)\
**Post date:** [June 3, 2020, 7:23pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/13 "2020-06-03T19:23:52Z")

</div>

I have a VB code that gets the URL from the Hyperlink in the spreadsheet. That takes me to a page in my applicate. Once the application is open to the correct invoice, I check to see if the invoice number matches, then preform a sequence of steps to fix the invoice error.

 ![image](https://global.discourse-cdn.com/uipath/original/3X/a/8/a84229369a4ed34503a60effecbd3ff074adc927.png) ![image](https://global.discourse-cdn.com/uipath/original/3X/2/b/2bb7b3cd6a778967d68b6a10ae110eb327b7891d.png)

---

<div class="post-metadata">

**Author:** ![Kristopher](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/kristopher/32/106943_2.png) [@Kristopher](https://forum.uipath.com/u/Kristopher)\
**Post date:** [June 3, 2020, 10:16pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/14 "2020-06-03T22:16:10Z")

</div>

Create a while loop that will attempt to scrape all of the data on the screen with relative selectors (e.g. find the selector for drop down box 1 - in UiExplorer, make sure it has idx as part of the selector)  
As you loop through the items on the screen, when you find the last item, stop the loop.  
You can probably accomplish the 2 steps you have highlighted in the loop when you encounter them by using that same idx and the relative selectors for the voucher and taxcode inputs.

---

<div class="post-metadata">

**Author:** ![bakerjan](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/bakerjan/32/176658_2.png) [@bakerjan](https://forum.uipath.com/u/bakerjan)\
**Post date:** [June 4, 2020, 10:42pm UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/15 "2020-06-04T22:42:53Z")

</div>

Thank you Kristopher, Sorry but I’m new to UIpath and this is my first project. My problem is I do not know what to put in the Condition when the number of iterations is unknow . Unless I’m missing something there is not a idx selector.

 ![image](https://global.discourse-cdn.com/uipath/original/3X/0/1/01043440bb9ca8395f3f3ec3ec26924ff393129e.png)

---

<div class="post-metadata">

**Author:** ![efelantti](https://avatars.discourse-cdn.com/v4/letter/e/9fc29f/32.png) [@efelantti](https://forum.uipath.com/u/efelantti)\
**Post date:** [October 20, 2023, 6:06am UTC](https://forum.uipath.com/t/how-to-break-a-while-loop-do-while-loop/13442/17 "2023-10-20T06:06:37Z")

</div>

@Yogeesh_G Kind note, if you choose to reply to a topic where the last answer was 3 years ago, it would be good to provide new information / ask a question related to it 🙂

Nowadays the `Break` activity works for While loops as well.
