UiPath While Example


UiPath While Loop Activity Explained

Welcome back. It’s been a while and that’s what I want to talk to you about right now. I want to talk to you about UI Path While loop. So I’m going to start off by creating a UI Path while loop example. I’m just going to call it UiPathWhileLoopExample, and not too much is going to happen here.

UiPath While loop Activity

I am just going to create a Blank process and then open the main workflow. In the main workflow, I’m going to drag a while loop onto the white space. And so, here’s my while loop here. The while loop has a couple of elements to it:

  1. Condition - it has to be something that evaluates to True or False.
  2. Body - which is just all of the logic that you want to implement.

The logic that I’m going to implement. Isn’t going to be too complicated. I’m just going to throw a message box on here that says You are in the UiPath while loop.

UiPath While loop Condition

I’ll leave the complicated RPA automations up to you. I just want to prove out how the UI Path while loop actually works. Now, there is a requirement with a while loop in order for a while loop to be entered, a condition has to be true. You can contrast that against the UI Path Do-while loop, where would the do while loop that loop is always entered once and then the condition is checked at the end of it.

With a while loop, if the condition isn’t true to start off with the while loop has never entered. Now you do need to have a condition and conditions are usually generated by evaluating variables. So I’m just going to create a variable here called UserInput, and then that’s going to be a String variable and that’s going to have scope for the entire sequence.

So, I want it visible everywhere inside of the while loop and outside of the while loop as well. And the user input is really going to be. Just answering the question, do you want to continue the loop or not? And so, the default value that I’m going to set is Yes. And so, you can see here, I’ve got that variable, it’s default value is yes.

And in the condition, I can just check, UserInput = “Yes”?. And if it is, this while loop will run. Now when you’ve got a while loop, you’ve got to have something happen inside the while loop. That’s going to flip the Boolean state of the condition from true to false, otherwise you will end up with an infinite loop. So, what I want to do next, let’s take input from the user. So drag a user input dialogue box over here. Maybe the title will be Welcome UI Path While Loop Lovers. And then the input will just be Do you want to continue the loop?, and then instead of a text box I’m going to do multiple choices, it’s pretty cool. With multiple choice, you can just write a bunch of text inside, double quotes, separated with a semi-colon and then those become the options.

So I can say ”Yes; No”. And then that’ll actually generate an input dialogue box with radio buttons for Yes or No, depending on which was clicked. The value that’s clicked will be assigned to the variable that’s associated with the input dialogue, which of course is going to be this UserInput variable here.

So, I can either specify that in Value entered or I can specify that as Result either way. There we go. Oh, I could specify it there. I notice that it pops up in this window. And now I have a very handsome while loop. I’ve got a variable UserInput. We checked to see if UserInput is Yes. If it is, we print out, “Hey, you are in the while loop”.

And then after we’ve done that, we then ask, “Hey, do you want to continue?” And it keeps continuing until we click Yes. So there we go. Save that UiPathWhileLoopExample, click run. The loop is entered and we see, “Hey, you are in the UI Path while loop.”. I click Ok. And we get the message box “Do you want to continue the loop? ”.

Yes. I’m having fun with this UI Path while loop. Okay. “You’re in the while loop”. “Do you wanna continue the UI Path while loop? ”, Yes I do, click Ok. “You’re in the while loop”. I’m getting a little bored now, I want to exit the UI Path while loop. So I say No, and then that should terminate the application and that terminates the application. Because the condition of the UI Path while loop becomes False. And if the condition of the while loop is false, the while loop exit, and then logic after the while loop picks up. And there you go. That’s an example of how to use the UI Path while loop.

UiPath If Activity Example
This UiPath while loop example shows you how to implement iterative logic in your RPA applications.

Sample Questions

Sample Question #1

Question: What statement best describes While Loop?

  •   Execution of the body first before checking the condition
  •   Checking the condition first and then execute the body

Answer

Option b is correct.

The option A is the definition for the Do-While loop, before it checks the condition, it initially runs the body. While on the on the other hand, the While loop checks the condition first before getting into the body.

Sample Question #2

Question: What type of state does the condition accept?

  •   True or False
  •   Boolean variable
  •   Int32
  •   1 or 0

Answer

Options A and B is correct. In the condition statement, it accepts a Boolean variable or True or False. This is can be contrasted to any decision-making statements like for example the If activity. As long as the statement inside it results to True, the body will execute, if it’s False, then it will skip the execution of the body.

Where to find out more about the UiPath While Loop activity?

If you’d like to go beyond this UiPath While Loop Example, visit the UiPath Forum.

Discuss with Us on Twitter
Get UiPath Certified