Skip to main content

Lesson 4: Project Flow with Nested Loops

With nested loops, the project flow can seem overwhelming. The project begins by checking the repetitions or conditions of the outer loop and then triggering the inner loop to begin.
The inner loop executes until all repetitions are completed or the condition is met, before returning back to the outer loop. This cycle repeats until all repetitions of the outer loop are completed or until the condition of the outer loop is met.

In the example below, the project begins by starting the first repetition of the outer loop. In this case, the outer loop repeats three times.

  • The first [Repeat until] block is the first inner loop.

    A diagram of the project flow to identify the outer and inner loop of the project from the previous page to move the three disks to the goals. The Repeat loop set to 3 is labeled as the outer loop, and the Repeat until loop to drive to the disk using the Down eye sensor is labeled as the Inner Loop.
  • The first inner loop will run until the condition is reported as TRUE. The project will then break from the first inner loop and begin moving through the rest of the blocks inside of the outer loop.

    A diagram of the project flow of the same project, this time focusing on the actions of the Inner loop, the Repeat until loop. The project will execute the inner loop, driving forward, until the Down Eye detects an object, at which point it will move on the rest of the blocks in the stack.
  • This continues until the second [Repeat until] block begins. This is the second inner loop.

    A diagram of the project flow of the same project, this time identifying the second inner loop - the Repeat until block used to Drive back to the goal using the Distance Sensor. When the project moves out of the first inner loop, the VR Robot will stop driving, pick up the disk, and turn around to face the goal. The second inner loop will then execute, causing the robot to drive forward until the Distance Sensor reports a value less than 200mm.
  • The blocks inside of the second inner loop will repeat until the condition of the block is met. The project will then break from the second inner loop and begin moving through the rest of the blocks inside of the outer loop.

    A diagram of the project flow of the same project, this time identifying flow between both inner loops. The first inner loop will make the robot repeatedly drive forward until the Down Eye sensor detects the object, then the blocks to pick up the Disk and turn to the goal will execute. Next, the second inner loop will cause the VR Robot to repeated drive forward until the Distance Sensor reports a value less than 200mm, then the blocks needed to drop the disk in the goal and turn to the next goal will execute.
  • Once all the blocks in the outer loop are complete, the project goes back to the beginning of the outer loop and repeats the process again.

    A diagram of the flow of the project showing how the outer loop repeats all of these behaviors three times, at which point the parameter of the outer Repeat loop, set to 3, reports as True, and the project breaks out of the outer loop.

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf