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 disk of each color in the matching color goal. The VR Robot is facing the right side wall at the edge of the green goal, in the lower right corner.

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 project.

What are Nested Loops?

In previous Units and Lessons in this course, you have used loops from the Control category such as the for loop, while loop, and infinite while loops to repeat behaviors. Placing one loop inside of another loop is called 'nesting'. Nested loops are helpful in projects where a VR Robot repeats the same behaviors more than once, like to move multiple disks on the Playground. Nesting loops can organize and condense a project, to make it more easily understood. This can be helpful to better understand the context and purpose of the code, as well as making it easier to identify errors and troubleshoot a project. 

For instance, to move all three blue disks to the blue goal, a for loop can be used to repeat the process of collecting a disk multiple times. The project shown here places the Lesson 3 project to move the first blue disk to the blue goal inside a for loop, with the parameter set to 3. Notice how the four sections of the previous project are indented within the for loop.

def main():
    for repeat_count in range(3):
        #1. Drive to the first disk
        while not down_eye.near_object():
            drivetrain.drive(FORWARD)
            wait(5, MSEC)
        drivetrain.stop()

        # 2. Pick up the disk
        magnet.energize(BOOST)

        # 3. Drive to the goal
        drivetrain.turn_to_heading(180, DEGREES)
        while front_distance.get_distance(MM) > 200:
            drivetrain.drive(FORWARD)
            wait(5, MSEC)
        drivetrain.stop()

        # 4. Drop the disk in the goal
        magnet.energize(DROP)

        drivetrain.drive_for(REVERSE, 100, MM)
        drivetrain.turn_to_heading(0, DEGREES)

    wait(5, MSEC)

When you 'nest' loops, the outer loop controls the number of times that the inner loop is executed.  In this project, the for loop is the outer loop, and will repeat the behaviors in the loop three times.

The project from above with the for loop highlighted in a red box. Indented beneath the main definition, the for repeat underscore count in range (3) colon is the start of the loop, and the four sections of the project are indented beneath that. Aligned with the for loop command at the bottom of the project is a wait command, showing where the loop ends.

The inner loops are those loops contained within the outer loop. In this example, each of the while loops (to drive to the disk and to the goal) are 'inner loops' that are executed each time the project moves through the for loop.The same project from above, with each of the inner while loops highlighted in a red box. The first and third comment sections of the project contain while loops to use the sensors on the robot to drive until a disk is detected, then until the wall is detected.

For Your Information

Indentation is important in VEXcode VR Python projects. You can use the gray indicator lines in the workspace as a visual aid when nesting loops, to ensure that your project works as intended. Each line indicates the start and end of a loop.

The same project from above with the guiding lines highlighted in red boxes, showing the indentation alignment of each of the inner loops in relation to the rest of the project. The for loop line extends straight to the final wait command. And each of the while loop lines extend to the stop commands.

Flow of a nested loop project 

In this example, each time the for loop iterates, a blue disk is picked up and returned to the blue goal. 

After the first iteration, the first blue disk is returned to the blue goal and the VR Robot is turned to be ready to collect the second disk.

A side by side image of the project from above shown in the workspace to the left, and the Playground Window shown to the right. The project has an orange arrow wrapping from the for loop command around the entire project, illustrating the first iteration of the for loop. On the Playground, the first blue disk is in the blue goal, and the VR Robot is positioned facing the remaining blue disks, at the front end of the goal, indicating the robot's position after the first iteration.

After the second iteration, the second blue disk is collected and the VR Robot is turned to be ready to collect the third disk.

A side by side image of the project from above shown in the workspace to the left, and the Playground Window shown to the right. The project has an orange arrow wrapping from the for loop command around the entire project, illustrating the second iteration of the for loop. On the Playground, 2 blue disks are in the blue goal, and the VR Robot is positioned facing the remaining blue disk, at the front end of the goal, indicating the robot's position after the second iteration.

After the third iteration, the third blue disk is collected, and the VR Robot stops moving. The for loop has now completed its range, so the project is completed. 

A side by side image of the project from above shown in the workspace to the left, and the Playground Window shown to the right. The project has an orange arrow wrapping from the for loop command around the entire project, illustrating the third iteration of the for loop. On the Playground, all three blue disks are in the blue goal, and the VR Robot is positioned facing away from the goal just outside the goal box, indicating the robot's position after the final iteration.

 

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