CS Level 1 - VEXcode VR Python

Unit 7 - Decisions with Colors

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

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

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.

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