Skip to content

Loops

Use the loop blocks to control the number of times a program executes lines of code.

Repeat [number of] times

This block repeats code a specified number of times.

Example 1

In this example, the //code.Node speaker plays 16 random frequencies for 0.5 seconds each.

Conditional repeat

The block repeats a task while a condition is valid or until it meets a condition.

Example 2: While loop

A while loop continues as long as a condition is valid. In this example, the LED remains set to cyan until the program stops.

Example 3: Until loop

The until statement keeps looping until it meets a condition. In this example, the loop is keeping the blue LED on. If the light sensor detects darkness, the loop stops and then turns the speaker on for 8 seconds.

Counting loop

This block counts up at a specified integer until it reaches the range limit.

Example 4

This loop initially sets the green LED's brightness to 1 and increments by 1 until it reaches 10. The LED stays at each brightness level for 2 seconds.

Example 5

This loop starts the speaker at a frequency of 200 Hz and increases by 100 Hz until the loop reaches 1400 Hz. Each frequency plays for 0.5 seconds.