UiPath If Then Tutorial

  • Introduction

    Hey, I’m Cameron Mackenzie. I’m the editor in chief at theserverside.com and you can follow me on Twitter @Cameronmcnz and I want to talk to you about UiPath programming and specifically how the if-then else activity works. So, I did a tutorial earlier on the number guesser, take a look at that.

    Using If-else statement in UiPath

    I did that purely with a do-while loop. In this example, I’m going to achieve the same ends, but I’m going to deal with an if-then-else statement. Nesting, some of those, if conditions inside of each other. So, if you want to learn the basics of conditional programming and UiPath, you’ve come to the right place. Now I’m going to implement the number guesser with an if-then nested, if then statements are going to create a New Project called IfthenNestNumberguesser. That’s a long enough name, a lot of the workspace to come up and as soon as it does open up, I’ll open up the main workflow window. I think, I am going to start off by adding in a do-while loop. So, that means going down into workflow, opening up the control element and adding a do-while loop here now actually want an infinite loop, but that sounds a little strange. But what I want to do is I want to keep asking the user, Hey, What is your pick? What number did you pick? What number did you pick?

    Break activity to break out of the loop

    I just want that to keep going on and on and on until they pick the right number and when they pick the right number, I’m going to break out of the loop and so this actually creates an infinite loop, which is kind of interesting and inside of this loop, I want to ask the user for some input. So that means going into system dialogue-> Input dialogue and dragging input dialogue onto the body here and I’m going to have a title. This will be in the title bar of the dialog box and we’ll say,
    ‘Pick a number between 1 and 10.’
    and we’ll say in the dialog box, ‘What is your number?’. Now, remember this has to all go within double quotes. If you don’t have double quotes here, you’re going to have a problem. So just, just make sure that’s in double quotes (“”), only variables go without the quotes.

    Storing the guessed number in a variable

    Speaking of variables, when somebody types in a number, we will actually want to store that as a variable. So that means I got to come down to this variables element here. I want to store what the user’s guess is, and it should be a number it’s not going to make int32 and I going to give scope for the entire sequence.

    Assign the correct scope to the variable

    If you don’t give scope for this entire sequence, that variable can only be used inside of the section in which it’s declared. If you give it scope for the sequence, it can be used anywhere inside of the big sequence block. Now we’ve got that variable guess declared when somebody types something into the input box. I want that value, that number, to be assigned to guess. So, I put that over here under output. What happens when somebody types something into that field? Well, that gets pushed out here into that variable. So now when somebody types in a number that guess field is going to be initialized.

    Assign the correct scope to the variable

    So, what do we want to do after that? Well, after that, I want to check to see if the number that they typed in was too low and so I drag an if block over, I’m not going to bring it from recent there. I’ll let you see where you can find it. It goes under, I always forget where it goes here. It goes under system activities-> statements, and then there’s that if statement there and when I drag it on, you’ll notice that it’s actually an if-then that’s all say if what’s the condition, the guess is less than 5 (guess<5).

    Evaluating the false case scenarios

    Then what do we want to do in that case? We’ll send them a message box that says guess higher and then what do we do if it’s not less than 5, that means it’s greater than 5. Then I guess we should say guess lower. The only problem is that possibility exists that they actually picked 5, which is the correct number. So, we’ve actually need to nest our if-then-else statements and that means throwing an if statement over here to the right, and then in this, if statement will say, if the guess is greater than 5 and we’ll tell them to guess lower and then what happens if their number is not less than 5, not greater than 5, if it actually is 5. Well, we can now put that into this else block here and that would be a message box saying, ‘You guessed it! It was …’ and then what the guess was because the guess is right. If we actually get to this point, that means that guess 5 guess is holding the value 5. So that it should print up- ‘You guessed it! It was 5’. Now we do have this issue about the infinite loop here and unfortunately, we don’t have infinite resources.

    Evaluating the true case scenarios

    So when somebody guesses the right answer, we actually want to break out of the loop and so how do you do that? All I have to do is go down to workflow control and you’ll find this break and if you drag the break over here, you notice it now says, okay, when somebody guesses it, right, we’ll say, Hey, you guessed it right. The number was what the guess was and then the break and that break forces you to break out of the current loop that you’re inside of. So that will kind of override this true because this condition won’t ever get executed. Once that break happens, we break out of the loop and then. Execution actually would follow along if there was anything below after the loop. But actually I can even improve that out by just putting a message box here after the condition and saying and so the message boxes, ‘Thanks for Playing!’. So that’ll show you that we break out of the loop, but it’s not a way of terminating the application. It’s not like an exception was thrown or something like that. Now, what did I do wrong now on that guess? I got to say guess.ToString. Now I always forget to do that.
    That’s an int and they don’t like, uh, you know, connecting int’s and strings if you’re doing strong typing. So it was one little thing that I forgot there, but otherwise I think this all looks really handsome. Yeah. I don’t know if that makes things any better or any worse, but that’s what it looks like all put together and let’s give this a run. So run the application. What’s your number type into guess higher what’s your number seven guess lower. What’s your number? I’ll type in 5. You guessed it. It was 5 that should trigger the break and then after the break, after the loop, it says, thanks for playing!!! and there you go. That’s how you can do some interesting flow control with nested loops.

    So there you go. Those are the ins and outs of nested if-then statements, if you enjoyed this tutorial, head over to the server side.com, where I’m the editor in chief, you can find lots of great articles and tutorials about enterprise software development over there. If you want to learn more about me, you can follow my antics on Twitter @Cameronmcnz and subscribe on YouTube.

UiPath If Then Else Activity Example

UiPath If Activity Example
This UiPath if then tutorial demonstrates how conditional logic is implemented in UiPath Studio.

Test Your Knowledge

  1. What is Break Activity used for ?
    • It is used to terminate the loop or statement in which it is present
    • It forces to execute the next iteration of the loop.
  2. What can be used to store a input value in UiPath Studio?
    • Variable
    • Argument
    • None of the above
  3. What is the VB Expression that is used to convert a variable to String type explicitly?
    • {variableName}.ToString

References that you might want to have a look at :

Where to find out more about UiPath If Then statements?

If you’d like to go beyond this UiPath If Then activity tutorial, visit the UiPath Forum.

Discuss with Us on Twitter
Get UiPath Certified