hey i'm cameron mackenzie i'm the editor-in-chief at the serverside.com and you can follow me on
twitter at cameronmcnz and i want to talk about uipath programming and specifically
how the if then else facility works so i did a tutorial earlier on the number guesser take a
look at that 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 do it 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 in uipath
you've come to the right place now i'm going to implement the number guesser with an if then
nested if then statement so i'm going to create a new project called the if then nest number guesser
that's a long enough name allow the workspace to come up and as soon as it does open up i'll
open up the main workflow window and i'm 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 i 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 what's your pick what number did you pick what number did you
pick 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 dialog input dialog and dragging input dialog onto the body
here i'm going to have a title this will be in the title bar of the dialog box and we'll say pick
pick a number between 1 and 10 and we'll say in the dialog box what is your number now remember
uh 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
speaking of variables when somebody types in a number we actually want to store that as a
variable so that means i've 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 so i'm going to make it int 32 and i'm going to give
scope for the entire sequence if you don't give a 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 i'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 in to 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 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 uh 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 and so i'll say if what's the condition the guess is less than
5 then what do we want to do in that case we'll send them a message box that says guess hire
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 the possibility exists that they actually
picked 5 which is the correct number so we've actually got 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 we'll say if the guess is greater than 5 then 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 they've guessed five guess is holding the value five so this should print
up you guessed it it was five now we do have this issue about the infinite loop here and
unfortunately we don't have infinite resources so when somebody guesses the right answer we actually
want to break out of the loop and so how do you do that all you 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 could even
prove that out by just putting a message box here after the condition and saying
and so now the message box says 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've got to say guest dot tostring you
always forget to do that that's an int and they don't like you know connecting ins and strings
if you're doing strong typing so there was one little thing that i forget 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 i'll run
the application what's your number type in two guess higher what's your number seven guess lower
what's your number i'll type in five you guessed it it was five 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 else statements if you enjoyed
this tutorial head over to theserverside.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 at cameronmcnz and subscribe on youtube