Loops in UiPath Studio

Loops are structures used to automate repetitive tasks. In flowcharts, the simplest types of loops can be created by connecting a certain point in the workflow to an earlier execution one.

In sequences, there are some special activities that repeat the action which is inside the body section. The UiPath studio is having three types of loops such as

  • While
  • Do While
  • For each

The While and Do While loops work by repeating a given set of actions from the body while the specified condition is true. The only difference between the two is the order in which the 2 elements are executed:

  • While : if the condition is met, the set of actions in the body are executed.
  • Do While : the actions are executed and then, if the condition is met, the actions are executed again.
  • The For Each loop works by iterating through a list of items, one item at a time, and executing whatever actions are in the body of the action.

Keyboard Shortcuts in UiPath Studio

While Loop in UiPath

You use the keyword while then a condition enclosed in parentheses, then a single statement, which is the thing which gets repeated as long as the condition is true.

If the body of the loop has to be more than one statement, enclose it in the curly brackets {}. This creates a compound statement, and the while will treat it as a single statement.

The following example demonstrates printing numbers from 1 to 100 by using while loop

As the first step we need to create a sequence, I am going to create a sequence by the name While_Loop_Example.
uipath-whie-loop-new

Drag and drop Assign activity inside the sequence and create a variable called Counter and assign the value Counter=1
assign-acticity-inside-while-loop-rpa-uipath

Add the while loop activity inside the sequence and write the condition(Counter<100) for the same as shown below.
uipath-whie-loop-new-example

The Counter=1 and the while loop checks whether the counter is less than 100. if it is less than 100 then UiPath prints the output as 1. As we are not incrementing the value of the counter here and hence this condition will execute infinite times.

Let us increment the value of the Counter by 1. Add the assign activity inside the while loop and write the condition as shown below.
uipath-whie-loop-new-example-2

Now save and run the sequence, once the sequence starts to execute, UiPath will print the numbers from 1-99 as shown below.
uipath-while-loop-new-output
the-complete-output-of-while-loop-rpa-uipath

Basics of Automation in UiPath Studio

Do While Loop in UiPath

In a do…while loop, the statements in the body of the loop are executed once before the condition governing the loop is tested. If the condition evaluates to true, the loop will iterate a second time executing the loop’s body again; otherwise, the loop exists because the condition has become false.

Example.1 :

The following example demonstrates the do-while loop

Create a sequence Do_while_example and add assign activity inside the sequence and then create a variable called Counter as shown below.
do-while-loop-uipath

Next, add Do While activity inside the sequence and write the condition for the same.
do-while-loop-activity-uipath

Save and run the sequence, Once the sequence starts executing, it will print the output as 100.
do-while-loop-activity-uipath-output

Keyboard Shortcuts in UiPath Studio

For Each Loop in UiPath

The For Each loop works by iterating through a list of items, one item at a time, and executing whatever actions are in the body of the action.

Example.1:

The following example demonstrates For each loop

Create a sequence called For_Each_Loop.
for-each-loop-uipath

Create a variable called names from variable pane as shown below, I am going to create an array of strings, so select the datatype as Array[T] and array list as names=["anu","aru","ashu"].

For creating an array of strings first select the Array[T] in the data type and then select the System.String[] from the Select Types wizard and then click on Ok.

select-datatype-rpa-uipath

Now, enter the array values and add For Each activity inside the sequence as shown below
creating-an-array-of-strings-rpa-uipath1

Now, save and run the sequence, Once the sequence starts to execute the array values will be printed one by one as shown below.
for-each-loop-uipath-output