The For Each loop works by iterating through a list of items, one at a time and executing whatever actions are placed within the body of the loop.
For example, you would use a For Each loop when you want to run the same set of activities for all invoices received that day.
The For Each loop works with all data collection types (e.g. Arrays, Lists), discussed here, except for DataTables. The For Each loop will not stop until all elements within the given collection have been processed.
What Does A For Each Loop Look Like?
How to use a For Each Loop in UiPath?
In UiPath, the For Each loop can be achieved using the For Each activity. The For Each activity can be used in both a sequence and a flowchart and works with all data collection types, except for DataTables. Let’s take a look at the For Each Activity:
- Input Data Collection – Contains the data collection you wish to loop through.
- Item Name – This is a local variable name for the object in the data collection you are currently processing. It is called a local variable as the scope is within the body of the loop only and the value is replaced every time a new object is selected from the Data Collection. Even though the default name is “item”, this name can be changed to anything and is not shown in the Variables Panel.
- Body – Contains the actions and activities you’re looking to repeat.
There are also additional properties that can be found in the properties panel:
- Argument Type – The data type for each object in the data collection. For example, an array of strings should have string as the argument type. The default argument type is set as object.
- Index – The zero-based index (i.e. starts at zero) of the current element in the collection. This can be useful for logging purposes for determining the progress of the loop.