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.

Parameters:

  • Count (Number): Number of times to repeat

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. The condition is checked after each iteration, so the loop always executes at least once.

Parameters:

  • Condition (Boolean): The condition to evaluate after each iteration

Example:

Read until valid value received

iterator

Returns the current iteration counter for the loop, starting from 0. Use this to create indexed operations or patterns.

Returns:

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

Example:

Alternate on/off pattern for multiple LEDs

Last updated