UiPath

About UiPath Looping activities

In UiPath Studio development, we often do iterative processes.

However, there are several activities that perform repetitive processing, and it is difficult to determine which activity is appropriate to use.

In this article, I will explain the “Repeat Number Of Times , For Each, For Each Row, While, do While, Flow Decision” activities that can be used for iterative processing, and how to use them appropriately.

This site was created by translating a blog created in Japanese into English using the DeepL translation.

Please forgive me if some of the English text is a little strange

 

Uses and target data for iterative processing activities

If you have a fixed number of repetitions, use “Repeat Number Of Times”.

Use “For Each” or “For Each Row” if you want to read all the target data.

If you want to repeat only when the specified condition is met, use “While”, “do While”, or “Flow Decision”.

The table below summarizes the results.

Activity Name Uses Data to be repeated
Repeat Number Of Times Repeats a set of activities a specified number of times. Any
For Each Performs an activity or a series of activities, specified in the Body section, on each element of a collection. Variables corresponding to iterative processes (IEnumerable variables)
For Each Row in Data Table Executes an action once for each row in a specified DataTable variable. DataTable Variables
While Checks if the expression inserted in the Condition field is True and then executes the activities in the Body section of the activity as long as it remains True. Any
do While Executes the activities in the Body section of the activity and then checks if the expression inserted in the Condition field is True. Any
Flow Decision An activity which executes one of two branches, depending on whether a specified condition is met. Any

 

Repeat Number Of Times

“Repeat Number Of Times” is used when the number of times to repeat is fixed.

The activity can be found in Workflow > Control

 

“Repeat Number Of Times” Setting item

Setting location Setting item Setting contents
In the Body of the Activity For each Enter a name by which to refer to the current iteration.
Repeat number of times select Number to enter the number of times to execute the activities added inside the activity.
In the Properties Panel Common DisplayName The name displayed for the activity in the Designer panel.
Input Number Of Times See Repeat number of times in the body of the activity.
Misc Private If selected, the data used in the activity is not logged by StudioX.
Options Start at Choose the value CurrentItem starts with. The default option is 1.

 

Sample Process
Repeat three times to output the data in list1 to the log.

 

・Description of each activity

  • “Assign List<String> Type” to assign [New List(of String) From{“apple”, “orange”, “banana”}] to list1.
  • By setting [Repeat number of times] of “Repeat Number Of Times” to 3, the Do will be executed 3 times.
  • The [CurrentItem] of “For each” will be assigned the number of times it has been executed.
  • “Log Message list1 contents”, the array of list starts from 0, so the number in () is set to [number of executions – 1].

 

・Code for copy and paste

lits1 = New List(of String) From{"apple", "orange","banana"}
CurrentItem.ToString+" time"
list1(CurrentItem-1)
"----------"

 

・Execution result

 

 

For Each

For Each is used when you want to read all the array data such as Array and List.

The activity can be found in Workflow > Control.

For Each  setting item

Setting location Setting item Setting contents
Body
For Each The elements can be referenced in the activities in the Body section by using the iterator variable, item.
in The variable whose values you want to iterate.
Properties Common DisplayName The display name of the activity.
Misc Private  If selected, the values of variables and arguments are no longer logged at Verbose level.
TypeArgument A drop-down list that enables you to choose what variable type you want to use with the activity.
Values The variable whose values you want to iterate.
Output Index A zero-based index that specifies which element of the current collection is being iterated, stored in an Int32 variable.

引用元:For Each

 

Sample Process
List type Displays all values in list1.

・For Each Properties

・Description of each activity

  • “Assign list<String> Type” to assign [New List(of String) From{“apple”, “orange”, “banana”}] to list1.
  • For each item in [For Each], the data taken from list1 will be assigned in order.
  • Set int32 type intCount to index in the output of the For Each property, and output the number of iterations in “Log Message Index”.
penguin-san
penguin-san
Please note that the number of repetitions of index is counted from zero.

 

・Code for copy and paste

lits1 = New List(of String) From{"apple", "orange","banana"}
"index: "+intCount.ToString
item
"------"

 

・Variables

 

・Execution result

 

For Each Row

Executes an action once for each row in a specified DataTable variable.

The activity can be found in Workflow > Control.

For Each Row  setting item

Setting location Setting item Setting contents
Body
For Each Enter a name to be used when referring to the current iteration. The default is row.
in The DataTable variable for which an action is to be executed once for each row
Properties Misc Private If selected, the values of variables and arguments are no longer logged at Verbose level.
Input DataTable The DataTable variable for which an action is to be executed once for each row
Common DisplayName The display name of the activity.
Output Index A zero-based index that specifies what element of the current collection is being iterated, stored in an Int32 variable.

 

Sample Process
Load the data in TestScore.csv into Data Table Type dt, and repeat each line in dt to output to the log.

・”data\TestScore.csv”

 

・For Each Row  Properties

・Description of each activity

  • In “Read CSV”, read “data\TestScore.csv” with its header and assign it to the Datatable type dt.
  • The “row” of “For Each Row” is assigned to the row data retrieved from dt in order.
  • Since “row” represents a row, we can use (0) or (1) to specify a column to display the loaded data.
  • Set int32 type intCount to index in the output of the “For Each Row” property, and output how many times it is repeated in [Log Message Index].
penguin-san
penguin-san
As with “For Each”, please note that the number of iterations for the current index is counted from zero.

 

・Code for copy and paste

"Index: "+intCount.ToString
"Name: " + row(0).ToString
"TestScore: " + row(1).ToString
"------"

 

・Variables

 

・Execution result

 

While

Checks if the expression inserted in the Condition field is True and then executes the activities in the Body section of the activity as long as it remains True.

The activity can be found in Workflow > Control.

 

While Setting item

Setting location Setting item Setting contents
Body
Condition As long as the condition specified in this field is True, the activities in the Body of the activity are executed.
Properties Misc Private If selected, the values of variables and arguments are no longer logged at Verbose level.
Input Condition As long as the condition specified in this field is True, the activities in the Body of the activity are executed.
Common DisplayName The display name of the activity.

引用元:While

 

Sample Process  1
While the count is less than 3, the data of the count in the list is displayed.

・Description of each activity

  • In [Assign List<String> Type], assign [New List(of String) From{“apple”, “orange”, “grape”}] to [list1].
  • By setting the Condition of [While] to [intCount<3], [Body] will be executed repeatedly as long as intCount is less than 3. If the condition is not satisfied, it will not be executed even once.
  • In [Assign Count up] under [Body] in [While], add intCount by 1 for each iteration.

 

・Code for copy and paste

lits1 = New List(of String) From{"apple", "orange","banana"}
intCount = 0
"Count: "+intCount.ToString
"list1 Value: "+list1(intCount).Tostring
intCount = intCount + 1
"------"

 

・Variables

 

・Execution result  1

 

 

Sample Process  2
Reads the data in “data\TestScore.csv” into Datatable type [dt] and outputs the data of each line of the count in [dt] to the log while the count is less than 3.

 

・Description of each activity

  • In [Read CSV], read “data\TestScore.csv” with its header and assign it to Datatable Type [dt].
  • By setting the Condition of [While] to [intCount<3], [Body] will be executed repeatedly as long as intCount is less than 3. If the condition is not satisfied, it will not be executed even once.
  • The dt(intCount)(0) in [Log Message Name] in [Body] of [While] represents column 0 of the intCount line of [dt].
  • In [Assign intCount Count Up] under [Body] in [While], add intCount by one for each iteration.

 

・Code for copy and paste

intCount = 0
"Count: "+intCount.ToString
"Name: :" + dt(IntCount)(0).ToString
"TestScore: :" + dt(intCount)(1).ToString
intCount = intCount + 1
"-----"

 

・Variables

 

・Execution result  2

 

do While

Executes the activities in the Body section of the activity and then checks if the expression inserted in the Condition field is True.

The activity can be found in Workflow > Control.

Do While  setting item

Setting location Setting item Setting contents
Body
Conditions As long as the condition specified in this field is True, the activities in the Body of the activity are executed.
Properties Misc Private If selected, the values of variables and arguments are no longer logged at Verbose level.
Input Conditions As long as the condition specified in this field is True, the activities in the Body of the activity are executed.
Common DisplayName The display name of the activity.

引用元:Do While

 

Sample Process
While the count is less than 3, the data of the count in the list is displayed.

・Description of each activity

  • In “Assign List<String> Type”, assign [New List(of String) From{“apple”, “orange”, “banana”}] to “list1”.
  • By setting [intCount<3] for “Conditions” in “Do While”, the text will be executed repeatedly as long as intCount is less than 3. Even if the condition is not satisfied, it will be executed once.
  • In “Assign Count Up” in the body of “Do While”, intCount is added by one for each iteration.

 

・Code for copy and paste

lits1 = New List(of String) From{"apple", "orange","banana"}
intCount = 0
"Count: "+intCount.ToString
"list1 Value: "+list1(intCount).Tostring
intCount = intCount + 1
"------"

 

・Execution result

 

Flow Decision

An activity which executes one of two branches, depending on whether a specified condition is met.

The activity can be found in Workflow > Flowchart.

Flow Decision setting item

Setting location Setting item Setting contents
Properties Misc Private If selected, the values of variables and arguments are no longer logged at Verbose level.
Conditions As long as the condition specified in this field is True, the activities in the Body of the activity are executed.
DisplayName The display name of the activity.
FalseLabel Enables you to provide a description of the case in which the condition is not met.

引用元:Flow Decision

 

Sample Process
While the count is less than 3, the data of the count in the list is displayed.

・Flow Decision  Properties

 

・Log Message



 

・Description of each activity

  • In “Assign List<String> Type”, assign [New List(of String) From{“apple”, “orange”, “banana”}] to “list1”.
  • By setting “Flow Decision” to intCount<3, the True condition on the left side will be executed repeatedly as long as intCount is less than 3.
  • In “Assign Count Up” in the body of “Do While”, intCount is added by one for each iteration.

 

・Code for copy and paste

lits1 = New List(of String) From{"apple", "orange","banana"}
intCount = 0
"Count: " + intCount.ToString
"True End""True End""Value: "+list1(intCount).Tostring
"------"
intCount = intCount + 1
"True End"

 

・Execution result

Summary

  • If you have a fixed number of repetitions, use “Repeat Number Of Times”.
  • If you want to read all the target data, use “For Each” or “For Each Row”.
  • If you want to repeat only when the specified condition is met, use “While”, “do While”, or “Flow Decision”.