Skip to main content

Lesson 4: Using Nested Loops

In the previous Lesson, you created a project to instruct the VR Robot to drive to, pick up, and move all three blue disks in the Disk Mover Playground into the blue goal. In this Lesson, you will create a project to pick up and move one disk of each color into its corresponding colored goal in the Disk Mover Playground using nested loops!

A top down view of the Disk Mover Playground, with the first of each blue, red, and green disk in their respective goals, and the VR Robot facing the edge of the Playground on the far right at the green goal.

Learning Outcomes

  • Identify how to nest loops.
  • Describe the flow of a project through nested loops.
  • Explain why nested loops would be used in a VEXcode VR project.

What are Loops?

Blocks from the Control category such as the [Repeat], [Repeat until], and [Forever] blocks repeat behaviors on a loop.

To the left is a project to draw a square that begins When started, move pen down. Next, a comment reads Drive in a square, and has a Repeat loop set to 4 with two block inside it to drive forward 600mm and turn right 90 degrees. To the right, is a top down image of the Art Canvas Playground with the VR Robot having drawn a 600mm square.

A [Forever] block, for example, repeats the blocks inside of it on a forever loop. The arrow at the bottom of the block indicates that the behaviors inside will be repeated on a loop.

A VEXcode VR Forever block from the Toolbox, with the arrow on the bottom of the C portion of the block highlighted to indicate the looping behavior of the block.

Using Switch Blocks 

This is the Switch [Forever] block. The Python command, while True: means that any behaviors nested inside of this conditional block will repeat forever, because the condition is set to be True. 

The Switch block equivalent of a Forever block, showing the Python command that reads while True:.

Select the Next button to continue with the rest of this Lesson.