# Looping

You can use looping to perform the same actions several times. It stops only after all the items have been looped.

The loop is useful when you want to process several similar elements, such as completing forms that values come from a Google sheet. You can use several methods of looping in GoLess:

1. The Loop Data blocks can be used to loop through a table, Google spreadsheet, variable, or custom JSON Array.
2. The Loop Elements block can be used to loop through elements on one page.
3. The Repeat Task block can be used for frequently repeating actions a specified number of times.

## Working with the Loop Data or Loop Elements Block

The Loop Break point must be included in the workflow when using the Loop Data or Loop Elements block. The Loop Break point lets you tell the workflow where the loop reach is. Also, inside the Loop Break point, you have to enter the loop block's loop ID.

![Loop with breakpoint](https://res.cloudinary.com/chat-story/image/upload/v1666320965/automa/chrome_7wEPnPfNRQ_e1ee2l.png)

The workflow above will constantly run the Forms and the Get Text block depending on the number of elements. After all elements are looped, the workflow moves to the Export Data block.

If you don't set the loop range using the Loop Break point block, the loop won't work.

![Loop without breakpoint](https://res.cloudinary.com/chat-story/image/upload/v1666320717/automa/chrome_csX5PLkZBq_h5hu0k.png)

The above example will run the Forms and the Get Text block one time and move on to the Export Data block.

### Access Loop Item

Expressions can be used to access data from the current loop iteration inside the loop range. For instance, replace the `loopId` with the Loop id you inputted inside the Loop Data or Loop Element block: `{{loopData.loopId}}`.

![Access loop item](https://res.cloudinary.com/chat-story/image/upload/v1666321501/automa/chrome_Z1IwOcaATY_gxfn6k.png)

The `{{loopData.loopId}}` expression will return:

```json
{
  "data": ...,
  "$index": 1
}
```

To sum it up, if you need to access the loop index, you can use expressions like`{{loopData.loopId.$index}}`. To get the loop value, you don't need to enter the `data` property like `{{loopId.loopId.data}}`, GoLess will automatically set it to the expressions. However, for JavaScript expressions, you should include the `data` property `!!{{loopData.loopId.data}}`

## Usage of the Repeat Task Block

The Repeat Task block is the simplest way to make a loop. Simply define how often to repeat the actions and where to start repeating them.

![Repeat task](https://res.cloudinary.com/chat-story/image/upload/v1666322277/automa/chrome_LWjlxa5ZMT_t2jrr2.png)

The above example will repeat executing the Click Element block three times. After that, the workflow will continue to the New Tab block.


---

# Agent Instructions: 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:

```
GET https://docs.goless.com/workflow/looping.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
