Skip to main content

Lesson 3: Using Conditionals

In the previous Lesson, you reached the end of the Disk Maze using the Eye Sensor and while loops. This Lesson introduces the if statement and how to use this statement in the Disk Maze Challenge.

A top-down view of the Disc Maze playground, with an arrow showing the intended path of the VR robot. The robot should drive through the entire course, turning right at green disks and turning left at blue ones to finally reach the red disk goal. The order is 1 green disk, 4 blue disks, 1 green disk, 1 blue disk, 1 green disk, and then the final red disk.

Learning Outcomes

  • Identify that the if statement runs the blocks inside of it if the Boolean condition is reported to be True.
  • Identify that you can have multiple if statements in a project to be able check for multiple colors, and have multiple behaviors based on those colors.

Observing Patterns and Using Conditional Statements

In Lesson 2, a pattern was identified for the way the VR Robot must drive when a specific color is reported by the Eye Sensor on the Disk Maze Playground.

  • Front Eye Sensor detects ‘GREEN’: True
    • Turn right 90 degrees

      A top-down view of the Disk Maze playground, with the intended path of the VR robot marked with arrows. Each turn following a green disk is highlighted, indicating that a right turn always follows a green disk.
  • Front Eye Sensor detects ‘BLUE’: True
    • Turn left 90 degrees

      A top-down view of the Disk Maze playground, with the intended path of the VR robot marked with arrows. Each turn following a blue disk is highlighted, indicating that a left turn always follows a blue disk.

This logic can be used to simplify a project with the if statement. If statements are conditional statements that instruct the VR Robot to make a decision if a specified condition is True.

if condition:
	pass

For Your Information

When an if statement is dragged into the Workspace from the Toolbox, a pass statement is automatically populated. The pass statement is a placeholder for a future command to be implemented. The if statement will need to have a command inside of it in order to run, so the pass statement is added. When you add commands to the if statement, they will replace the pass statement. 
If statement in the workspace showing how the pass statement is replaced with a different command, in this case the replacement is a turn for command.

Disk Maze Pattern with if statements

If statements in coding follow familiar logic. For example, you have probably used if statements to make decision in your life – "If it is raining, then I use an umbrella." or "If I see a stop sign, then I stop moving." The same pattern we identified in the Disk Maze using while loops, can be reframed to a series of if statements. 

  • If the Front Eye Sensor detects ‘GREEN', then turn right 90 degrees.A top-down view of the Disk Maze playground, with the intended path of the VR robot marked with arrows. Each turn following a green disk is highlighted, indicating that a right turn always follows a green disk.
  • If the Front Eye Sensor detects 'BLUE', then turn left 90 degrees.A top-down view of the Disk Maze playground, with the intended path of the VR robot marked with arrows. Each turn following a blue disk is highlighted, indicating that a left turn always follows a blue disk.

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