> For the complete documentation index, see [llms.txt](https://docs.kognitos.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kognitos.com/legacy/legacy-experience/writing-automations/automation-basics/loops.md).

# 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
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kognitos.com/legacy/legacy-experience/writing-automations/automation-basics/loops.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
