Skip to main content

Lesson 4: Using Infinite While Loops

Solving the Disk Maze Problem

Building the project

  • Load the Unit7Lesson3 project from the previous Lesson, or recreate the project shown here to begin.
    Unit 7 Lesson 3 code
  • Rename the project Unit7Lesson4.Unit7Lesson4

     

  • Drag in or type an infinite while loop to the top of the project, and set the condition to True.
    set condition as true
  • Move the wait command to make space for the commands that will go inside the infinite while loop. image of the project with the while loop added and the wait moved down
  • Inside the infinite while loop, copy and paste the previous if statements inside the loop. Do not delete the wait command at the bottom of the while loop. Your project may look like this image. Notice that copying and pasting commands in this way does not result in correct indentation.image of the project with the if statements copied into the infinite while loop
  • Remember, indentation is VERY important in VEXcode VR Python. In order for the project flow to execute as intended, the if statements need to be indented within the infinite while loop. Select the if statements and press 'Tab' to indent them within the infinite while loop. When properly indented, your project should look like this: 
    image of the project when the commands are correctly indented within the infinite while loop
  • With the infinite while loop added to the project, all of the if statements will repeatedly have their conditions checked. Open the Disk Maze Playground and run the project.
  • When this project is run, the VR Robot will navigate the Disk Maze Playground from start to finish. Once the VR Robot detects red, it will stop driving.
    maze

In Summary

  • In the project from the previous Lesson, the conditions within the if statements were only checked once. Since the conditions within the if statements were only checked once, the VR Robot drove forward forever because the condition of 'no color' being detected was reported as True at the beginning of the project.
  • Now that the infinite while loop has been added, each condition of the if statements will be checked repeatedly. The condition of the while loop is always True, and therefore the code inside the while loop will always execute. This will repeatedly check the conditions of the if statements while the project is running.image of the project flow of the infinite while loop 
  • During the flow of the project, if the condition of the if statements is True, the commands inside of the if statement are executed. If the condition of the if statement is False, the commands inside of the if statement are not executed, and the flow of the project will continue to the next command. This enables the VR Robot to perform discrete behaviors such as turning or stopping once it detects a certain color.
    multiple if statements vs. infinite while loop
  • If statements are used with infinite while loops to ensure that conditions are constantly checked.project flow of infinite while loop

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf