> For the complete documentation index, see [llms.txt](https://docs.midio.com/midio-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.midio.com/midio-docs/guides/loops-1.md).

# Loops

Looping in Midio is as simple as connecting nodes in a loop. This can however easily create infinite loops if we don’t branch out of the loop at some point.&#x20;

There are two main helper functions in Midio that makes it easy to prevent infinite loops:

* [`For`](/midio-docs/built-in-nodes/core-std.md#for) - used to loop a given number of times
* [`For Each`](/midio-docs/built-in-nodes/core-std.md#foreach) - used to loop once per item in a list

## For

<figure><img src="https://1896308808-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRdFpuRAnTVYgmlCXLLou%2Fuploads%2FTN9Huv7ZS1xzSFsCjCZG%2Fimage.png?alt=media&amp;token=37b97407-cf1e-43c0-80b7-8b34a79ba1ba" alt=""><figcaption></figcaption></figure>

The [`For`](/midio-docs/built-in-nodes/core-std.md#for) node allows you to repeat a sequence of nodes a set number of times, determined by the `iterations` input.

1. **Start the loop** by triggering the `reset` input.
2. [`For`](/midio-docs/built-in-nodes/core-std.md#for) will then activate the `on count` output trigger **once per iteration**, up to the specified number of times.
3. To continue looping, connect `on count` to the next node(s) in your sequence, and then back to the `count` input of [`For`](/midio-docs/built-in-nodes/core-std.md#for). This advances to the next iteration.
4. Once all iterations are complete, the `done` output trigger is activated.

## For Each

<figure><img src="https://1896308808-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRdFpuRAnTVYgmlCXLLou%2Fuploads%2FaMiCWhhNzjruCr8dJJTI%2Fimage.png?alt=media&amp;token=06edf5ea-cd1f-4b2b-9511-9fa784721c67" alt=""><figcaption></figcaption></figure>

The [`For Each`](/midio-docs/built-in-nodes/core-std.md#foreach) node allows you to loop through a list of items, executing a sequence of actions for each item in the list. Unlike a standard [`For`](/midio-docs/built-in-nodes/core-std.md#for)  loop that runs a fixed number of times, [`For Each`](/midio-docs/built-in-nodes/core-std.md#foreach) automatically iterates over every element in a list, passing along both the `current item` and its `index` in the list.

1. **Trigger the loop** by activating the `reset` input.
2. For each item in the list, [`For Each`](/midio-docs/built-in-nodes/core-std.md#foreach) activates the `on item` output trigger.
3. The `item` **output** provides the value of the current element, while the `index` **output** gives its position in the list.
4. Once all items have been processed, the **done** output trigger is activated.
