Categories
RPA UiPath

Control Flow (UiRPA part 5)

Control Flow in Studio

Control Flow in Studio

Overview

The Control Flow in Studio course is an intermediate course that describes how the control flow activities are executed throughout the process. You’ll learn how to use the If, While, Do While, For Each, Switch, and Parallel activities to control the order in which activities are executed within a process. 

What you will learn in this course

  • Explain what control flow is and it is important
  • List the most common control flow statements used in UiPath. 
  • Explain the most common control flow statements used in UiPath.  
  • Differentiate between the most common control flow statements used in UiPath.  
  • Configure the most common control flow statements according to their specifications. 
  • Explain what a parallel Activity is and how to use it. 

Important take outs

Control flow is one of the fundamental concepts in programming. This describes how the activities are executed in a process or workflow. Think of the order in which they are executed, the conditions, the repetitions, and so on.

What is a control flow?

It is the order in which activities are executed or evaluated in a software project. There are two concepts through which the control flow is enacted.

The workflow layouts

There are four predefined workflow layouts – Sequence, Flowchart, State Machine, and Global Exception Handler.

State Machine is a type of automation that uses a finite number of states in its execution. It can go into a state when it’s triggered by an activity, and when another activity is triggered, it exits that state. 

The Global Exception Handler is a type of workflow designed to determine the project’s behaviour when encountering an execution error.

In Sequences, the process steps flow in a clear succession. Decision trees are rarely used. Activities in sequences are easier to read and maintain, and thus they’re highly recommended for simple, linear workflows.

In Flowcharts, the individual activities are a bit more difficult to read and edit, but the flows between them are much clearer. Use flowcharts when decision points and branching are needed to accommodate complex scenarios and decision mechanisms.

Sequence, Flowchart, State Machine, Global Handler

The control flow statements

These are activities and methods that define the decisions to be made during the execution of a workflow. The most common control flow statements are IfWhileDo WhileFor EachSwitch, and Parallel

The graphic below highlights the most important control flow statements available in UiPath Studio. They are of 3 types:

  • The conditional, also known as decision-making activities such as If, Else If, and Switch.
  • Loops such as While, Do While, and For Each.
  • Parallel Activity which executes child activities asynchronously, in parallel.
Control Activities

What is the importance of control flow?

  • Control flow takes care of the order in which the actions are performed in automation.
  • Activities like If/Else If, For Each, enable us to define rules and automate conditional statements within the project.
  • Loops enable us to run a part of the automation a specific number of times until a condition is met, or indefinitely.

The IF Statement

In UiPath, the If statement or If Activity is made up of the elements you’d expect it to be: 

  • The Condition that’s verified (with two potential outcomes – true or false).
  • The Then branch – is the set of actions to be executed when the condition is true.
  • The Else branch – is the set of actions to be executed when the condition is false.

What’s different is that, based on the chosen type of layout, there are two corresponding activities that fulfil the If statement role:

  • The If activity in sequences makes decisions based on the value of the variable in use.
  • The Flow Decision activity in flowcharts executes one of the two branches, based on the condition met.

Moreover, the VB.NET If operator can be used inside expressions. The Else If Activity in UiPath Studio is mainly designed for sequences. It comes with the ability to add multiple conditions to the Else branch.

  • If the first condition is met, then you perform the action under it.  
  • If not, you can add another condition using the second Else If and if the condition is True, then perform the action under it. If not, add another condition using Else If.
  • Else, if none of the conditions are met, then you perform the last action under Else.

The graphic shows the Else If Activity with its two drop-down options of Else If and Else. The advantage of Else If Activity is, it makes the design clear and easy to follow.

If and Else If

Example

  1. ((Year mod 4 = 0) AND (Year mod 100 <> 0))
  2. if ((Year mod 4 = 0), “Message for TRUE”, “Message for FALSE”)

About Loops

Loops are repetitions of a set of operations based on a given condition. In UiPath, the most important loops are: Do While, While and For Each.

Do While

It executes a specific sequence while a condition is met. The condition is evaluated after each execution of the statements. For example, a robot could perform a refresh command on a website and then check if a relevant element was loaded. It will continue the refresh – check cycle until the element is loaded. The key difference between Do While and While is that for Do While, the activities contained within it are executed at least once. The graphic depicts how a Do While loop appears in a simple workflow in UiPath Studio.

Do While activity in UiPath

While

It executes a specific sequence while a condition is met. The condition is evaluated before each execution of the statements. For example, if a Robot would play Blackjack, it should calculate the hand before deciding whether to draw another card. The graphic depicts how a While loop appears in a simple workflow in UiPath Studio.

While activity in UiPath

For Each

It performs an activity or a series of activities on each element of an input collection. This is very useful in the data processing. Consider an Array of integers. For Each would enable the robot to check whether each numeric item fulfils a certain condition.

For Each activity in UiPath

How do you exit a loop?

Loops are fun, but you should know how to stop the ride at some point. There are multiple ways to exit a loop:

  • The While and Do While loops will exit when upon checking, the condition is no longer true.
  • The For Each loop will exit when it has finished processing every item in the input collection.
  • The Break activity enables you to exit the current loop activity (e.g. For Each, While, or Do While) at a chosen point and continues the workflow with the activity that follows it.

Differences between For Each and While

Now that we’ve seen the loops in action, let’s tabulate some of the differences between them.

For Each loopWhile loop
Performs an activity or series of activities on each element of an enumeration.Executes contained activities while the condition is True.
Iterates through each element in the collection.Iterates as long as the condition is met.
Exits upon completion of processing of each item in the collection.Exits upon checking if the condition is false.

Switch

It is a type of control flow statement that executes a set of activities out of multiple, based on the value of a specific expression. In other words, we use it instead of an If statement when we need at least three potential courses of action. This is done through the condition, which is not Boolean like in the case of If statement, but multiple.

What are some business scenarios in which I will use Switch?

  • An invoice that has three potential statuses (not started, pending, approved) and three sets of actions for each one.
  • A process of automatically ordering raw materials to four suppliers based on certain conditions.

Differences between Switch and If

Here’s a table showing some of the differences between switch and if statements.

SwitchIf
Useful when we need at least three potential sources of action.Useful only with two potential courses of action.
Uses a condition that is not boolean. But enables the selection of one choice out of multiple, based on the value of a specified expression. Uses a condition that is boolean in nature.
Workflow design with multiple nested If-Else replaced by switch activity is easy to follow.Workflow design with multiple nested If-Else looks cluttered and difficult to follow.
By default, the switch activity uses an integer argument in the Case Value section in Studio.If statement uses an integer or character.

Parallel Activity

The Parallel activity enables you to execute two or more child activity branches at the same time. In UiPath Studio, the Parallel activity can be found in the Activities panel, under Workflow Control > Parallel. The Parallel activity finishes only after all child activities are complete or when its CompletionCondition property evaluates to true. 

Why is it important?

The Parallel activity is very beneficial when there are one or more blocking activities in the branches, because it executes all child activities simultaneously and asynchronously, thus saving time and being very efficient.

Where do I use it?

Let’s imagine a Sequence workflow that contains two other Sequence activities. 

The first sequence must download a .pdf file on a webpage and uses a ‘Wait for Download’ activity that waits for the download to complete before any further processing of the file in the automation. 

The second sequence gets data from an invoice and sends the output by email to a specific recipient.

What the Parallel activity does is that it won’t stop the automation to wait for the blocking activity to finish downloading, instead, it will switch between the child activity branches until they all finish executing or when its CompletionCondition property evaluates to true.

Tips & Facts

How to create an empty Array in UiPath: new string(){}

How to create an empty List: new List(of string)

How to display the values of an Array: string.Join(“, “, Array)

Parallel Activity performs its execution asynchronously from top to bottom, left to right.

The Switch activity can be useful to categorize data according to a custom number of cases.

Flowcharts can be used in a variety of settings, from large jobs to small projects that you can reuse in other projects.

The For Each activity enables you to step through arrays, lists, data tables or other types of collections so that you can iterate through the data and process each piece of information individually.

Else If activity lets you add multiple conditions using the drop-down options Else If and Else.

The Break activity can only be used within one of the following activities: For Each, While, or Do While.

Do While activities are evaluated only after the body has been executed once.

Congratulation

Diploma of completion
Diploma of completion
Congratulation, well done my Sir