Skip to main content

Lesson 4: Repeated Behaviors

In the previous Lesson, you observed how to use if statements in order for the VR Robot to check conditions. However, because of the flow of the project, the conditions within the if statements are only checked once. In this Lesson, you will be introduced to an infinite loop and how it can be used to solve the Disk Maze Challenge.

Same project flow from the previous page with the four if statements and arrows indicating the conditions each if statement is checking and that the only statement that reports as True is the front eye detecting no color, which means the robot will drive forward.

Learning Outcomes

  • Identify that an infinite while loop repeats the behaviors inside the loop forever.
  • Describe why an infinite while loop would be used in a project.
  • Describe the project flow in a project that has multiple if statements inside of an infinite while loop.
  • Explain why if statements are used with infinite while loops.

Repeatedly Checking Conditions

As observed in the previous Lesson, the if statement only checks a condition once. In order for conditions contained in the if statements to be checked repeatedly, the project needs to loop back to the start over and over again. To do this, an infinite while loop is needed.

while True:
	wait (5, MSEC)

An infinite while loop uses a condition that always evaluates to True. While loops execute the body of the loop when the condition is True, so the commands inside the loop repeat infinitely.

The infinite while loop and the Disk Maze Problem

To make the VR Robot behave as we intend it to, and successfully navigate the Disk Maze, the conditions set by the if statements must be repeatedly checked as the VR Robot drives around the Playground. Adding an infinite while loop to the whole project will enable this to happen because all if statements contained in the infinite while loop in the project below are repeatedly checked until the project is stopped.

Note the indentation here. All of the if statements are indented to be within the infinite while loop. The end of the infinite while loop is noted by the wait command.

def main():
    while True:
        
        # If front eye detects green then turn right
        if front_eye.detect(GREEN):
            drivetrain.turn_for(RIGHT, 90, DEGREES)

        # If front eye detects blue then turn left
        if front_eye.detect(BLUE):
            drivetrain.turn_for(LEFT, 90, DEGREES)

        # If front eye detects red then stop
        if front_eye.detect(RED): 
            drivetrain.stop()

        # If front eye detects none then drive forward
        if front_eye.detect(NONE): 
            drivetrain.drive(FORWARD)

        wait(5, MSEC)

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 infinite while loop is always True, and therefore the code inside the infinite while loop will always execute. This will continuously check the conditions of the if statements while the project is running. 
    • Remember that the ‘inside’ of a while loop is indicated by indentation.
  • During the flow of the project, if the condition of the if statement 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.
  • If statements are used with infinite while loops to ensure that conditions are constantly checked, essentially combining the project flow of the if statements within that of the loop.

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