Skip to main content

Lesson 2: If/Else Conditional Statements

In this Lesson, you will create a project where the VR Robot knocks over at least two buildings on the Dynamic Castle Crasher Playground. An algorithm will be needed to solve the challenge using loops and sensor feedback because the Playground layout changes with each reset.

Examples of Dynamic Castle Crasher Playground Layouts:

Three top down views of sample layouts of the Dynamic Castle Crasher Playground with the robot in the same starting position and castles in different positions. The layouts are side by side, to emphasize how the castles change position each time the Playground is reset.

Learning Outcomes

  • Identify that an [If then else] block is a C block that runs the blocks inside either the If or the Else branch based on the Boolean value reported.
  • Identify that an [If then else] block is normally used with a [Repeat] or a [Forever] block in order to check the condition more than once.
  • Identify and describe why an [If then else] block is used with loops.
  • Describe what would cause an [If then else] branch to be run in a project.

How to Use an [If then else] Block

This challenge is different from the Castle Crasher challenge in Unit 2. The Unit 2 challenge used simple sequencing and Drivetrain commands to knock over the buildings on a static playground.

A sample VEXcode VR project from Unit 2. The project begins with a When started block and has 6 blocks attached. The blocks read, in order, Set drive velocity to 100%; Set turn velocity to 100%; drive forward for 1550mm; turn right for 180 degrees; drive forward for 700mm; then turn right for 90 degrees.

Drivetrain commands alone will not be sufficient to have the VR Robot knock down two or more buildings because of the changing layouts of the Dynamic Castle Crasher Playground. A set of Drivetrain commands may work in one Playground, but not in another. An algorithm that uses sensors and selection will be needed. The [If then else] block will be used to create an algorithm by adding selection with conditional statements to this project.

A top down view of the Dynamic Castle Crasher Playground with only some castles knocked over, and others untouched, to illustrate how drivetrain commands alone are insufficient when the position of the castles changes randomly.

There are two main steps for the VR Robot to knock over buildings on the Dynamic Castle Crasher Playground.

  1. If the VR Robot detects a castle building, drive forward towards it to knock it over.
    1. Unlike every other Playground, the Dynamic Castle Crasher Playground does not have walls. Because the buildings are the only objects on the Dynamic Castle Crasher Playground, the Distance Sensor can be used to detect buildings.

      A top down view of the Dynamic Castle Crasher Playground with the robot in the starting position, at the bottom center of the layout, with a dotted arrow pointing to a castle straight in front of the robot that is highlighted in a red box. The arrow indicates that the sensor can detect the castle as an object in front of it.
  2. If the VR Robot does not detect a castle building, turn and check for a castle building again.
    1. Because the buildings are the only objects on the Dynamic Castle Crasher Playground, the Distance Sensor can be used to detect castle buildings.

      A top down view of the Dynamic Castle Crasher Playground, with the robot in the starting position, and curved arrows on either side of it, indicating the need to turn until a castle object is detected.

Once steps 1 and 2 are complete, the VR Robot can go back to step 1 and use the Distance Sensor to find the next building to knock over.

  • Start a new project in VEXcode VR and name the project Unit9Lesson2.

    Project name box in the center of the VEXcode VR Toolbar highlighted with a red box, to the left of the Select Playground button. The project name reads Unit 9 Lesson 2.
  • Add two [Comment] blocks into the workspace and fill in the steps outlined above.

    A new VEXcode VR project with two Comment blocks attached to the When started block. The comments read in order: Knock over the building found using the Distance Sensor; and Turn to find a building using the Distance Sensor.
  • An [If then else] block can be used to trigger each of the actions outlined above. Each action depends on if the Boolean condition in the [If then else] block reports TRUE or FALSE. Add an [If then else] block beneath the [Comment] blocks.

    The same VEXcode VR project with an If then else block attached below the second comment. The parameter of the If branch is left open.

    For Your Information

    The [If then else] block is a C block that accepts Boolean reporter blocks as input. The [If then else] block is a conditional statement that controls the flow of a project. The [If then else] block will execute a particular sequence of instruction if the condition is met, and another set of instructions if the condition is NOT met, thereby ‘branching’ the project flow. Only one branch in the [If then else] will be executed.

    An If then else block from the VEXcode VR Toolbox.
    • If the condition is reported as TRUE, the blocks inside of the If Then branch are run.
    • If the condition is reported as FALSE, the blocks inside of the Else branch are run.
    A VEXcode VR project is used to illustrate the function of each branch of the If then else block. In this project the If then else block is attached to a When started and reads If Front Distance found an object, then Comment - Knock over the building found using the Distance Sensor and drive forward; Else, Turn to find a building using the Distance Sensor, and Turn right. The 'If then' branch is labeled Drive forward if Distance Sensor detects an object. The 'Else' branch is labeled as Turn right if distance sensor does not detect an object.

    Once all blocks inside one of the branches are run, the project will move on to the next block outside of the [If then else] block.

  • In this project, the Distance Sensor can be used to detect buildings on the Playground. Add in a <Distance found object> block to the [If then else] block.

    The same VEXcode VR project from before, with a Distance Found object block added as the parameter of the If then branch. The If then block now reads If Front Distance found an object then.

    For Your Information

    The <Distance found object> block is a Boolean reporter block that reports if the Distance Sensor has found an object in front of the VR Robot. This block reports TRUE when there is an object or surface within 3000 millimeters (mm) of the VR Robot.

    Distance found object VEXcode VR block from the Toolbox that reads Front Distance found an object?
  • Now each branch of the [If then else] block needs to be defined using the comments created at the beginning of the project. When the <Distance found object> block reports TRUE, the VR Robot should drive towards that building to knock it over. Move that [Comment] block to the ‘If Then’ branch of the project.

    The same VEXcode VR project with the first Comment moved inside the C of the If then branch. The project now reads When Started, If Front Distance found an object, then Knock over the building found using the Distance Sensor.
  • Move the other [Comment] block to the ‘Else’ branch of the project for what should happen if the VR Robot does NOT detect a building.

    The same VEXcode VR project with the second comment inside the C of the else branch. The project now reads When started, If Front Distance found an object, then Knock over the building found using the Distance Sensor; else, Turn to find a building using the Distance Sensor.
  • Add a [Drive] block into the ‘If Then’ branch of the [If then else] block.

    The same VEXcode VR project with a Drive block set to forward beneath the first comment in the If then branch. The If then branch now reads, If Front Distance found an object, then Comment Knock over the building found using the Distance sensor; Drive forward.
  • When the <Distance found object> block reports FALSE, the VR Robot will need to turn around and find a building on the Playground. Add a [Turn] block beneath the [Comment] block in the ‘Else’ branch of the [If then else] block.

    The same VEXcode VR project with a Turn block set to right beneath the second comment in the Else branch. The Else branch now reads, Else, Comment of turn to find a building using the Distance Sensor; then Turn right.
  • Before the project is tested, one more block needs to be added. [If then else] blocks will only check the condition once before moving to the next block in the stack. In Unit 7, a [Forever] block was added to the project to instruct the VR Robot to repeatedly check the condition of the Down Eye Sensor. To ensure the Distance Sensor condition is being checked repeatedly, drag a [Forever] block into the workspace around the [If then else] block.

    An overview of how to add a Forever block to the project so the entirety of the If then else block is inside the C of the Forever block. The project now reads When started, Forever, If Front distance found an object then drive forward; else turn right.
  • Open the Dynamic Castle Crasher Playground and run the project.

    A top down view of the Castle Crasher Playground with three castles intact and two castle buildings knocked over. The robot is in the upper right hand corner beside a toppled castle.
  • Once the VR Robot has knocked over at least two buildings, reload the Playground and run the project on another layout of the Dynamic Castle Crasher Playground.

    A top down view of the Castle Crasher Playground with three castles intact and two castle buildings knocked over. The robot is in the upper right hand corner beside a toppled castle.
  • On each run of the project, the VR Robot will turn until a building is detected by the Distance Sensor, then drive towards that building to knock it over.

    A side view of the VR Robot pushing a castle piece off the side of the Playground, but the robot is beginning to drive onto the border of the Playground as though it too will fall of the edge.
  • During the run of the project, the VR Robot may push a building piece all the way to the edge and fall off the Playground. This is because the <Distance found object> block is still reporting that there is an object in front of the VR Robot. When this block reports TRUE, the VR Robot drives forward. Leading the VR Robot to fall off the table.

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