Hello.
Welcome to the second part of our introductory training, Control Flow. in the previous episode
you learned about the basics of UIPath, variables, and how actions are contained inside flowcharts
or sequences.
If you don't yet have a firm understanding of these basic concepts, make sure you watch
the previous training again, because today we will dive deeper into the mechanics of
UIPath; specifically... control-flow.
Control-flow is the process of defining the rules and automatic decisions your workflow
will take, through the use of if/else decisions, loops, and so on.
So without much further ado, let's start with probably the most important piece of computer programming,
the IF/ELSE decision.
You've seen it briefly-mentioned in the previous training, but this time we're gonna try it
out.
Actually, we're going to do the same example twice, because the IF-else decision is one
of the very few that has two virtually identical activities: one for the flowchart mode, and
one for the sequence mode.
They may look different but they work exactly the same: by taking one of the two different
actions, based on a specific condition.
That may sound a bit more complicated than it actually is, so let's see a simple example:
we’ll make a workflow that checks if a certain year is Leap Year or not.
It does that by testing if the specific year is a multiple of 4.
So, starting with a blank project, we’ll first drag in a flowchart, like we learned
in the previous training.
We’ll name this MasterFlowchart.
It will help us keep things organized.
Then we’ll add another flowchart inside it, connect it to the start node... and double-click
to edit it.
And it's a good idea to name this one too.
That's it for the setting-up part.
Now to the real automation.
First, we need to create a new variable to hold our specific year.
For that, go to the variables pane and create a new variable called year.
Since it will hold a number, we’ll change the type from string to integer; and also
give it a value of... 2013, for testing.
Now; because we are in a flowchart, we’ll drag in the flow-decision action, and not
the IF action.
How do we test if a number is a multiple of 4?
There are a few options but the easiest is the modulus operator.
It works by giving us the remainder of a division operation.
In case you forgot that piece of math, the remainder is the number "left over" after
dividing one integer by another.
So, to see if a number is a multiple of 4, we can look at the remainder.
If it’s 0, the number is a multiple of 4, which means it's a leap year.
Coming back to our flowchart, first connect the decision node to the start node.
And in the Condition field we’ll put Year – the variable we created earlier – mod
4 = 0.
And that's it.
Now add a new message box action by dragging it from the left pane.
And connect it to the decision node.
This being the situation when the year is a ,multiple of 4, we’ll just say… “this
is a leap year”.
And drag another message box.
But this time don't drop it yet, hover over the Decision box, and drop it on this blueish
arrow.
It means it will automatically connect here.
For this one we’ll display...
"This oneisn’t”.
So let's review.
The program starts from the {start node} and goes to {Decision box}.
Here, if the variable Year that we defined earlier is a multiple of 4 it will display
{this} message box, And if it's not – {this other} box.
When we run we see the Year…
2013 is not a leap year...
... and that {2016} is.
Since we don't want to change this variable by hand every time, we'll just add an input
dialog, by dragging it on this wire, before the decision.
This inserts the action between the two.
This is what an empty input dialog does.
Not much, just gives you an empty field where we can type.
We can customize its properties here; and for starters we’ll change the label to...
“please enter year”; in quotations.
All text strings are in quotations.
What the input field does is it takes the value of what a user types and outputs it
to a variable.
since we already have a variable defined we'll just put its name here; or just the first
few letters because the rest of the name autocompletes.
Now, although at the very start of this program the value in the {year variable was 2016},
after the input dialog, its new value will be whatever we type in the empty field.
So if we put in 2015, we'll see that this one isn't a leap year.
And this one is.
Great.
And finally lets give these some meaningful names, and correct this spelling mistake.
So that's how the decision node, message box and input dialog work.
Now let's quickly recreate this exact behavior in a sequence so you can see what that’s
like.
So just go to the MasterFlowchart by clicking here, drag in a new sequence,
and give it an appropriate name.
We’ll do almost the same steps.
First, add the input dialog, and create the Year variable by right-clicking in the output
field; it’s a bit faster.
And change its type to Integer.
Unlike before, this time drag in an IF block, and drop it in the sequence.
For the condition, we’ll put the same thing as before: Year mod 4 = 0.
Also add a couple of message-boxes for the True and False situations, and type the same
messages as before.
As you can see, the sequence IF is very similar to the flowchart decision node.
The notable difference is that the IF action is more suitable for small pieces of automation,
because there is only so much you can fit in the tiny Then and Else boxes {hl}.
Although technically, you could drop in a container, like a flowchart or sequence, and
have a whole process in there.
But the decision node is more suitable for this kind of higher-level decisions, business
choices, and so on.
Don't worry you'll start to get a feel for the subtle difference between the two once
you start automating more complex projects.
But for that, you’ll also need...
LOOPS.
Loops are structures most often used to automate repetitive tasks.
Since repeating similar operations without making errors is a core function of automation,
and programming in genreal, loops are an essential element in most projects.
In UIPath There are a few methods for repeating certain tasks.
The simplest is available in flowchart mode and it involves connecting
a certain point in our flowchart to an earlier execution point.
And in sequence mode, there are looping containers, that repeat the actions there are inside their
Body section.
In our flowchart let's say we want to start the process again when we haven't found a
leap year.
To do that, we’ll just connect the end of this branch of the workflow, to where we want
the execution to resume.
Now, when the program reaches this message box, it will go to the start of the program,
and ask us again to input a number.
But if the number is a leap year, the program chooses the true branch of the workflow, and
the execution will stop after the message box.
As you can see it's pretty easy to make a loop in a flowchart simply by connecting a
wire to a previous execution point.
Just make sure there’s also an exit option out of the loop, otherwise your automation
will be forever stuck in that loop.
So that's how looping works in flowchart mode.
In a sequence there are another couple of options: the do/while loops and foreach.
It's pretty clear that the while and doWhile loops are almost the same thing.
They work by repeating the set of actions in the {body} while the {condition} is true.
The only difference between the two is the order in which the 2 elements are executed
For the While loop, if the condition is met, the set of actions in the body are executed.
For the do/while loop, the actions are executed and then, if the condition is met, the actions
are executed again.
The foreach loop is a bit different.
It works by iterating through a list of items, one item at a time, and executing whatever
actions are in the {Body} of the action.
But let's see some examples of all this.
Coming back to our seq leap-year workflow, let’s modify this example to repeatedly
ask for a number until you enter leap year, or a number that’s a multiple of 4.
We already have most of it, we just want to repeat it until we get it right.
So all we have to do is put all this inside a do-while loop and we're done.
So let's drag one inside the sequence, doesn't really matter where.
And then move the 2 existing actions inside the loop.
You can select both of them using the Ctrl key.
The do-while block got a bit bigger to accommodate the new actions, but it's still the same.
Now we just need to enter a condition for when this whole sequence to execute again.
and that will be when our {Year} variable is not a multiple of 4.
So we'll just copy condition from the IF statement and use for the While condition.
and change it to be not equal.
By the way, {this} is the symbol for not equal.
So now when we run the workflow the process stops only when we enter a leap year.
In our case the do-while action was preferable, but other situations might require the other
while loop, with the condition before the action.
Right.
I guess that's all pretty straightforward, so let's take a quick look at the for-each
loop we mentioned earlier.
We’ll use it to cycle through all the files in a chosen folder, and print out their name,
one by one; easy enough.
In practice you would probably do more advanced stuff than simply printing their name, but
now we’re just testing the forEach loop.
We’ll start by adding a new sequence [“read folders”] in the MasterWorkflow, and inside
it add a select folder action.
It's just an input dialog with that will let us choose a specific folder.
And create a new output variable by right-clicking and choosing create variable; we’ll name
it selectedFolder.
Then we’ll drop in an assign action.
on the left, create a new variable called filesList.
We want to get a list of files, and there's a special command for that: Directory.GetFiles,
and the folder’s path as a parameter.
In our case, we'll just use the one generated by the {selectFolder} action.
Because the getFiles command generates a list – or an array – of files, the type of
the variable must be array of strings.
Finally, add the for-each loop, and fill-in the list with our list of files.
This way, for each item in our list, we’ll do some action; in this case,
a simple write line, for each individual {item}.
Looks like we’ll also have to convert it to string.
So now if we run the automation, it will first ask us to {select the folder}, then UIPath
will {read that folder}, and using this .NET piece of code {hl}, it will give us a list
of files.
And we store this list in our variable, named simply filesList.
And then this loop will go through each item in the list – which in our case is a file
path – and print it to the output window.
[run] And here they are.
{output}
Goood.
We covered quite a few things in this video, so let's try a new exercise that will tie
together all the new things we learned.
We want to automate a kids guessing game.
The computer chooses a random number between 0 and 1000 and we will try to guess it.
It will respond with hints – like higher and lower – depending on if your guess was
smaller or larger than the random number.
And so on till you get it right.
So we could break it down to this: First, generate a random number Between 0
and 1000.
Then have the user guess the number.
if the guess was correct respond with a message and stop
and if not respond with higher or lower depending on user’s guess
Then repeat the whole process if the user guessed wrong
So, back in our MasterFlowchart, let's add a new flow chart, and name it Guessing Game.
Connect it to the start node so it will be the one that actually runs, and dbl clk it
to edit.
Let's start with the hardest part, generating a random number.
we’ll do that by assigning to a new variable, called Random, this value: new Random.Next,
between 0 and 1000.
It is a .Net command that you can easily find if you Google something like...
“.Net random number“.
Okay.
So, now that you have the only missing piece of the puzzle, you can stop the video and
try solving it yourself.
If you need to, rewind the video for hints.
Then after you're done, you can resume the video and see how we did it, and compare.
K, moving on with our solution.
Now we have our random number, we need to ask the user to guess it.
we’ll go ahead and add a new input dialog, and fill in the, title and label.
[guessing game, guess the #]
And the important part, the output variable.
We’ll create a new one called guess.
Now that we have the random number and user’s guess, we’ll add a decision node and test
whether the guess was correct.
So for the condition we’ll put guess = random.
For the true Branch add a message box saying that is correct.
Now for the false branch, we have to output two different messages, depending on a condition.
so drag in another decision node and connect it to the false branch of this decision node.
For the condition we'll put: guess < random.
This way, we'll know which hint to display.
And by the way you can click on these small yellow arrows to have the condition displayed
at all times.
We could have two different messages for the true and false branches of the decision, but
then you’d have 2 message boxes one after the other: one of the two hints {hl somehow},
and then the input dialog.
Here’s a more elegant solution.
We’ll create a new variable using an assign action, and store in it the hint.
So Right click... new variable... we’ll name it hint.
And the hint for the true branch is… “guess a bigger #”.
And for the other..
“Guess a smaller #”.
Now we’ll connect these 2 assign blocks to the input-box.
This way, if you guessed higher or lower than the correct number, the process will start
again from the input dialog.
[select input]
There’s one last thing to do.
And that is change the display message from the current “what's the number” question
to the hint we generated here {assigns}.
So we'll just put here, Hint.
Now when we run the workflow, the message box has no text, and that’s because the
hint variable doesn't yet have a value yet.
But after the first guess the hint is initialised & displayed.
We could easily solve that by giving it {initial value}, but I’ll leave that to you.
That's all for this episode of control flow.
As a second exercise, try creating the guessing game we just made, but in a sequence.
it should be fairly easy with what you learned today.
And make sure you tune in for the next episode, Recording, where you’ll see how UIPath generates
a lot of the work for you.
I’ll see u then.
bye!