> For the complete documentation index, see [llms.txt](https://docs.uniot.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.uniot.io/platform/sandbox/visual-editor/loops.md).

# Loops

Loop blocks repeatedly execute code, eliminating the need to duplicate instructions. Use loops to process multiple sensor readings, generate patterns, handle collections of data, or create sequences of operations.

## repeat

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-9274a1ba9e2c4c30ca10a39f285b932f2abb8257%2Floops_repeat.svg?alt=media" alt=""><figcaption></figcaption></figure></div>

Executes the enclosed code a fixed number of times. Use this when you know exactly how many iterations you need. Inside it, the [iterator](#iterator) tells you which pass is running.

**Parameters:**

* **Count** (Number): Number of times to repeat. It is checked before every pass, so a count taken from a variable that the loop itself changes changes the number of passes too.

**Example:**

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-849ce8417c627ec1b17ba693103506fe8f357192%2Floops_repeat_example.svg?alt=media" alt=""><figcaption><p>Average sensor reading</p></figcaption></figure></div>

## repeat while

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-868eb50337a5094b4d691b612ff48c3c33ebce3b%2Floops_repeat_while.svg?alt=media" alt=""><figcaption></figcaption></figure></div>

Repeats code as long as the condition remains true. The condition is checked before each iteration.

**Parameters:**

* **Condition** (Boolean): The condition to evaluate before each iteration

**Example:**

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-dd2f76819a33d5172a9dee84f45d1156105c7f6a%2Floops_repeat_while_example.svg?alt=media" alt=""><figcaption><p>Process values while above threshold</p></figcaption></figure></div>

## repeat until

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-a4af7e444a7215b11f08e4038d211bc2440a3fd4%2Floops_repeat_until.svg?alt=media" alt=""><figcaption></figcaption></figure></div>

Repeats code until the condition becomes true. Like **repeat while**, it checks the condition before each pass, so if the condition is already true, the code inside doesn't run at all.

**Parameters:**

* **Condition** (Boolean): The condition to evaluate before each iteration

**Example:**

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-ce18fb9de5442ee8089a0c366e260fc0e019ddb2%2Floops_repeat_until_example.svg?alt=media" alt=""><figcaption><p>Read until valid value received</p></figcaption></figure></div>

## iterator

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-5a56da9950bf5b1b35e8743f7f158287bf65d6ef%2Floops_iterator.svg?alt=media" alt=""><figcaption></figcaption></figure></div>

Returns how many passes the loop around it has completed: `0` on the first pass, `1` on the second, and so on. It works in all three loops. Use it to create indexed operations or patterns.

**Returns:**

* **Number**: The current loop iteration (0, 1, 2, ...)

**Nested loops:** the iterator belongs to the innermost loop around it. Inside an inner loop it counts that loop's passes, starting again from `0` each time the inner loop starts. When the inner loop finishes, the iterator is the outer loop's count again. To use the outer loop's count inside the inner loop, store it in a variable before the inner loop starts:

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-bb508795c066c7aa36280eb47be9460350a8551e%2Floops_nested_example.svg?alt=media" alt=""><figcaption><p>Print the cells of a 3 × 4 grid, numbered 0 to 11</p></figcaption></figure></div>

```lisp
(define row ())

(task 1 1000 '
 (progn
  (while
   (< #itr 3)
   (setq row
    (+ #itr 0))
   (while
    (< #itr 4)
    (print
     (+
      (* row 4) #itr))))))
```

{% hint style="warning" %}
**Known issue:** a variable set to the **iterator** keeps a link to the loop's counter rather than its value, so it changes along with the counter. In the example above, `row` would follow the inner loop, and a count saved inside a loop reads `0` once the loop has finished. Until this is fixed, store a copy instead — **iterator + 0**, as the example does: the arithmetic makes a new number. The fix is coming in the next release of the UniotLisp interpreter, after which the iterator can be stored directly.
{% endhint %}

**Example:**

<div align="left"><figure><img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-acfce49a2fc414f66852cfd65ee12d559f1406e2%2Floops_iterator_example.svg?alt=media" alt=""><figcaption><p>Alternate on/off pattern for multiple LEDs</p></figcaption></figure></div>

{% hint style="warning" %}
This block only works inside a loop. Anywhere else, and in a **repeat** block's count, the editor disables it and marks it with a warning. A disabled block is left out of the code, and the block around it falls back to its own default. In the example below, the comparison gets an empty value, `()`, and stops the script with `< takes only numbers`; a **set** block would quietly store `0` instead.

<img src="https://1776027180-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsSE1iwGRAGqUrb5YXfFa%2Fuploads%2Fgit-blob-d47ee967aa94dd9c9d53973f3e09eec377a235ce%2Floops_iterator_outside.svg?alt=media" alt="" data-size="original">
{% endhint %}

## How long a loop may run

A loop, together with any loops inside it, may run for 20,000 passes in all. Past that, the script stops with `Loop ran for 20000 iterations without ending; possible endless loop`. Something that should go on for as long as the device runs belongs in the [task](/platform/sandbox/visual-editor/special.md#task), which repeats on its own.
