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.
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.
The repeat block is found in the Logic - Control category.
repeat (10)
end
Connect the repeat block to the when started block.
Drag the drive for and turn for blocks inside of the repeat block.
Select the repeat number to change how many times the blocks within the repeat block will run.
If you want to repeat a sequence of actions forever, use 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.
break