Using Loops#

Using Loops

This tutorial covers how to repeat blocks using loops in VEXcode IQ (2nd gen). By the end, you’ll know how to use the repeat, forever, and break blocks.

If you want your robot to repeat the same action multiple times, like driving in a square, you could right-click the blocks and select Duplicate from the context menu, and keep duplicating those blocks until they move in the pattern of a square. A simpler way, however, is to use the repeat block within the Logic - Control category.

Right-click a block to open its context menu.

Screenshot of a block's right-click context menu, with Duplicate highlighted.

Select Duplicate to make a copy of the block, and any blocks connected below it. Repeat this until the blocks are duplicated as many times as you need.

Screenshot of a duplicated drive for and turn for block being dragged below the original blocks.

The repeat block is found in the Logic - Control category.

The repeat block.#
    repeat (10)
    end

Connect the repeat block to the when started block.

Screenshot of a repeat block being connected below a when started block.

Drag the drive for and turn for blocks inside of the repeat block.

Screenshot of a drive for block being dragged inside of a repeat block.

Select the repeat number to change how many times the blocks within the repeat block will run.

Screenshot of the repeat block's number highlighted, set to 4.

If you want to repeat a sequence of actions forever, use the forever block.

The forever block.#
    forever
    end

To break out of a loop, use the break block within a loop, to make the code exit the looping block immediately.

The break block.#
    break