UiPath Conditional and Iterative Logic

  • Introduction

    Hey, Cameron Mackenzie here at @CameronMcnz on Twitter and I wanted to talk to you about do while loops and UiPath. So, to start this project off, I am going to create a new process and then call it the number guessing game, little blank process there and as soon as the project is created, I am going to click on this button to open the main workflow.

    Get Guess input from User

    And what I am going to do is I am just going to keep asking a user, hey, pick a number between one and 10 until they have picked the number 5. In order to do an activity like that, where somebody keeps doing something until a condition is achieved your best to use a do while activity. So, I have gone into the workflow control and do while section and added this activity here.
    Of course in this activity, what do I want to do? Well, I want to keep asking the user to pick a number between 1 and 10 and so in order to ask for input, what you need is you need to go into the system area into dialogues and you can find the input dialogue.Now with the input dialogue, let me say, say something like, in quotes, and then in quotes, again, make sure this is all in quotes. This is going to have a little dialogue boxes to say, pick a number between 1 and 10 and then that is the title and then it is actually going to say in the dialog box, “What’s the number?”. Now, when this runs, it can take the input that the user types in and store it as a variable. You can see this little, you know, what is the output of running this input dialogue box. Well, it is, uh, the information somebody typed in which we can store as a variable. Now, in order to store variables, you need to actually declare them. So, there’s this little variables tab down here. You can see imports, arguments, and variables down here.

    Create a Variable:Guess

    I am just going to create a variable named Guess. It is going to be of type always we’ll make it of type int the scope will be for the entire sequence and so that’s the sequence. So, if we make it Sequence scope, the variable is available inside the sequence to do while, the body, if you make it, um, you know, less general, you know, you might not be able to use the variable in different parts of the sequence. So that’s why I made it sequence. So that’s the Guess and you can see right here in that input dialogue box, the result of somebody’s interacting with that dialog box and typing, something in is the initialization or setting of that variable. So, when somebody types something into the dialog box, it’s going to update this variable Guess.

    Specify Condition

    Okay. So, what is the condition? Well, we’re going to keep running this do-while loop until they’ve picked the right number. So, we’ll keep doing it while they’ve got the wrong number and so that would be something like Guess is not equal to, 5, right? Because we’re just going to say 5 is the magic number.
    <h3>Success Message</h3> Then when that do-while loop is completed, you can see that sort of the scope of the do while loop there. I want to do another activity in this case. I just want to say to them, Hey, ‘You guessed the right number’. So, I’ll add an Activity here or I can add this message box activity. Another way you can do this, as you can find the message box here and just add this on top and what we can do here is we can say, ‘You picked the right number! It was’, and then you can do something like 5 and there we go pick the right number, you know, I guess we could do it was and then plus their Guess. Right. Cause they’ve guessed it correctly. So I can say guess.toString and that gives you a bit of an idea of how you can append sort of a variable to an output message there and that’s the whole thing.
    So that’s the basic sequence. We’ll see how much of this I can get on the screen all at once lower that there’s the sequence, we say during the sequence we had to do a do-while loop inside this while loop, we asked the number, keep picking a number between 1 and 10. Yeah. If they guessed the number 5, we say, hey, you picked it. Right. And we use that variable to print that result out to them. Okay. I think that all looks good. Double check that you’ve initialized that variable and so that looks good. Make sure it’s declared down here, Guess and Guess. And when this is done, I can actually run the file and you can see it says what’s the number and I’ll say 1 and it comes back again. We’ll say 6and it comes back again, and I’ll say 3 and it comes back again and I say 5. And then finally it says ‘You picked the right number! It was 5’. I click OK. Then the application completes and there you go.

    That’s a nice introduction to a basic sequence project that uses workflow and there you go. That’s the ins and outs of a do while loop. Now stick around. I’m actually going to enhance this little number guesser application with some conditional statements and a couple of other variables. So please follow along. I think you’ll learn a lot about try-catch blocks, conditional statements, if then else’s and all those other things, it’ll make you a great UiPath programmer. Anyways, if you want to learn more head over to the server side.com where I’m the editor in chief, if you’re interested in my personal antics, you can follow me on Twitter @CameronMcnz and subscribe on YouTube.

UiPath Programming Challenge

UiPath If Activity Example
Learn iterative and conditional logic in UiPath by working through this Number Guesser example.

Test Your Knowledge

  1. Which activity is used to display information to the user?
    • Message Box
    • CallOut
  2. What can be the possible error, if the user is not able to access a variable?
    • Incorrect Scope assigned to the variable
    • Variable Data Type Conflict
    • Both a and b

References that you might want to have a look at :

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

If you’d like to go beyond this UiPath Conditional and Iterative logic challenge, visit the UiPath Forum.

Discuss with Us on Twitter
Get UiPath Certified