Skip to main content

Lesson 3: Using If Statements

Navigating the Disk Maze Using If Statements

In the Disk Maze Challenge, the VR Robot is checking the condition of the Front Eye Sensor to see what color is being reported. Each of the colors aligns to a different behavior the VR Robot should perform.

For Your Information

The if statement accepts only Boolean conditions. Using the patterns observed while solving the Disk Maze in Lesson 2, a project can be created where the VR Robot makes a decision if the condition reports as True or False.

condition of if statement

The if statement will run one time in a project. In this example, the Front Eye Sensor will report True if a green object is detected, and execute the command inside the if statement - turn right for 90 degrees. If a green object is not detected, the project will move to the next command.

if front eye detect green turn right
  • Start a new project and name it Unit7Lesson3.Unit7Lesson3

     

  • Add comments to describe the intention for the behaviors of the VR Robot during each following section of the project. image of the comments in the workspace to organize the project
  • Drag in or type the if statement beneath the first comment.
    empty if statement in the project
  • Set the condition of the if statement to the front_eye.detect command, and set the parameter to 'GREEN.'
    front eye detect green condition
  • Drag in or type a turn_for command inside the if statement, to replace the 'pass'. Set the parameters to turn right 90 degrees. Now, if the Front Eye Sensor detects 'GREEN', the VR Robot will turn right 90 degrees.
    turn for command inside if statement
  • We will follow these same steps to add additional if statements for all three colors in the Disk Maze (green, blue, and red). Each color is associated with a different behavior. The commands inside the if statements will need to match that intended behavior. Add the following commands to the project, so that if the Front Eye Sensor detects the color 'BLUE,' the VR Robot will turn left 90 degrees.
    if front eye detect blue turn left
  • When the VR Robot reaches the end of the maze, the Front Eye Sensor will detect the color ‘RED.’
    detects red
  • To stop the VR Robot, another if statement needs to be added with instructions for what to do if the Front Eye Sensor detects ‘RED.’ Add the following comment and commands to the project so that the VR Robot stops driving if it detects 'RED.'
    if front eye detect red stop driving

If None of the Colors are Detected

The VR Robot can also make a decision if it does not see a color by using the ‘none’ value. For instance, at the starting point of the Disk Maze, the Front Eye Sensor does not detect any colors.

Front Eye

Because the Front Eye Sensor does not detect a color at the beginning of the Disk Maze, the VR Robot will need to drive forward until it detects the green disk. Another if statement can be added to instruct the VR Robot for what to do when no color is detected. 

  • Add the following comment and commands to the project, so that if the Front Eye Sensor does not detect any color, the VR Robot will drive forward.
    add code if front eye detects none
  • The VR Robot now has instructions for each color detected by the Front Eye Sensor.
    • If Front Eye Sensor detects 'GREEN' – Turn right 90 degrees
    • If Front Eye Sensor detects ‘BLUE' –  Turn left 90 degrees
    • If Front Eye Sensor detects ‘RED’ – Stop driving
    • If Front Eye Sensor detects ‘NONE’ – Drive forward

  • Open the Disk Maze Playground and run the project. Does the VR Robot behave as you intended it to? Why or why not? On the next page of the Lesson, we will learn more about this project.

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