# Loops

### Overview

A loop allows you to repeat actions for multiple items. In Kognitos, you can create a loop using `process each x as follows`, where `x` represents the item in a set. Loops are especially useful for batch processing, where you need to handle multiple items in a consistent manner.

### General Syntax

To start a loop, use the following syntax:

```undefined
process each {item} as follows
  {actions to perform on each item}
```

By indenting the lines after `process each {item} as follows`, Kognitos will know which instructions to follow for each item.

{% hint style="success" %}
**Indentation is Key**

Remembering to indent when looping through objects in your automation is very important!
{% endhint %}

#### Example

```undefined
process each document as follows
  get the document's invoice number
  get the document's fields
```

***

### Looping with a Counter

To track the number of iterations in a loop, you can use a counter. A counter is a numeric data element that starts at an initial value *(usually zero)* and increments with each iteration of the loop. This allows you to keep track of how many times the loop has executed.

#### Syntax

Initialize the counter *before* starting the loop. Within each iteration, increment the counter by 1.

```undefined
the counter is 0
process each {item} as follows
 {actions to perform on each item}
 add 1 to the counter
```

#### Example

In this example, a loop is used to iterate through each page in a document. The counter is incremented after each page is processed.

```undefined
the counter is 0
process each page as follows
  get the page's invoice number
  get the page's booking reference
  add 1 to the counter
```
