How to break WHILE loop?

x=1, y=5

While (x<=y){
x=x+1;
}

How to break this WHILE loop? as it’s going to be an infinite loop?
I’ve tried putting break condition but it didn’t work. Please help.

@anjasing
As the programmer, it’s up to you to make sure that the loop doesn’t go on infinitely.

In this specific case - unless I am reading this wrong - the x = x + 1 expression will eventually end up where x = 6 and the while loop won’t continue anymore.

Are you talking about a different situation you have where it may go into an infinite loop?

Also, Continue and Break are used in For Each loop. For While or Do While loops, managing the exit criteria is the best approach.

Could you please elaborate further on Exit condition.
You mentioned right it should’ve end there…but it’s going infinite. Could you please help here?

Hi @anjasing

–keep the while loop within a TRY block of TRY CATCH ACTIVITY
–then inside the while loop use an IF condition with any condition that you would like to have so that the while loop will be broken and come out of it
—so if the condition is true it will go to THEN apart where we can use THROW activity and mention a BusinessException like this
new BusinessException(“break while”)
So that it will get broken and goes to a catch block where we can just mention an exception type like System.Exception and can mention with any activity or leave it as it is.

Regards
Gokul

Why would it be an infinite loop? Once x is 6 it will stop because “x <= y” is now false. Post a screenshot of your activity.

1 Like

Can you please share the screenshot of your while loop?

Exit criteria is the condition that will make sure that your while loop comes to a finite end.
In your example, x<=y is the exit criteria.

Also, you may not have to necessarily throw a Business Exception.

Assume that instead of checking for a numerical value as exit criteria, you want to check in UiPath whether an element is loaded. (e.g. keep checking if a button it becomes available or clickable)
In this situation, you could use Check App State activity to check for the element or Get Attribute activity to check certain attribute e.g. visibility attribute.
Depending on the activity you’re using, you can use the output as exit criteria.

  1. Check App State
    Use Result value as exit criteria, which is already a Boolean variable.

    Here ‘Check App State’ checks for Notepad window to be open.NotepadVisible is a Boolean variable, which will result false if Notepad window hasn’t loaded.
  2. Get Attribute
    Use Result value as exit criteria, depending on its datatype.

    In this example, visibility is an attribute that has numeric values. If the value is 1, it means the element is visible. This activity checks for the value and compares it to 1. Until the element is visible, the while loop will continue.

You can manage the exit criteria using one of these two activities (and other activities or steps that will make sure that these things happen - e.g. Open Notepad.exe).

I hope that makes it a bit clearer.