For the complete documentation index, see llms.txt. This page is also available as Markdown.

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

Executes the enclosed code a fixed number of times. Use this when you know exactly how many iterations you need. Inside it, the 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:

Average sensor reading

repeat while

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:

Process values while above threshold

repeat until

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:

Read until valid value received

iterator

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:

Print the cells of a 3 × 4 grid, numbered 0 to 11

Example:

Alternate on/off pattern for multiple LEDs

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, which repeats on its own.

Last updated