Skip to main content

Lesson 3: Conditional Statements

Lesson 3: Conditional Statements

In the previous Lesson, you learned how to use data from the Optical Sensor in a project to sort a Disk by color. In this Lesson, you will build on what you learned to explore options in VEXcode that enable the project to make a decision no matter what color is detected by the Optical Sensor. You will learn about: 

  • Why optimizing decision making is important in an industrial setting.
  • The difference between the If thenIf then else, and Else if blocks.

Checking Multiple Conditions

In the previous Lesson you created a project that could sort a Disk by color to a red pallet or a green pallet. While this is effective, the project could only sort one Disk at a time. In a factory setting, many objects need to be sorted and palletized repeatedly. In order to do this effectively, the 6-Axis Arm needs to be able to repeatedly make a decision on how to move the objects, or if human intervention is needed. Consider the Cubes you moved in the Introduction to the 6-Axis Arm course.Close up of the 6-Axis Arm holding a blue cube over the optical sensor.

What if a blue Cube was in the loading zone? What would happen in the project from the previous Lesson? In an industrial setting, all of the possible conditions need to be considered. If an errant object is mixed into the objects to be sorted, human intervention would likely be needed to ensure that the workcell and the workers around it stay safe and functioning effectively.

Sequential If then blocks

In the previous Lesson, you used two If then blocks in sequence to check two separate conditions one after the other, essentially asking two questions within the project.

Example VEXcode Project that would complete the challenge in the previous Lesson. A red highlight is on the comment that reads Move the Disk to the correct pallet and the following two If then blocks under that comment. The blocks in the red highlight box read as follows: if optical 5 detects red then move arm 10 to position x 169 y 150 z 100, and if optical 5 detects green then move arm 10 to position x 172 y 1 z 100.

First, Is the object red?, then, Is the object green?. Each condition is checked every time the project is run, regardless of what is detected by the Optical Sensor. While this structure enabled you to sort one Disk at a time effectively, it is not the most efficient way to do so. A red Disk could be sorted, but the Optical Sensor could also detect green from something else in the environment, and vice versa. There is also no consideration given for an object that is neither red nor green, like an unexpected object that could cause a safety issue.

Project Flow Images

Throughout this Lesson, images explaining the flow of VEXcode projects are shared. You can recreate these images in your engineering notebook as well as you move through the Lesson. 

Use tools in Google Slides or Microsoft PowerPoint like arrows, boxes, or flowcharts to help create visualizations that work best for you in your notebook.

Line toolbox in Google Slides open. The toolbox shows the options for Line, Arrow, Elbow Connector, Curved Connector, Curve, Polyline, and Scribble.

 

Sequential If then else blocks

There is a different block that could be used to enable us to both check for the red and green conditions and account for a different color. The If then else block is a C block that runs the blocks inside either the if branch or the else branch, based on the Boolean value reported.

Snippet of code using an if then else block with red and green arrows to denote project flow. The project reads: move disk to the correct pallet, if optical 5 detects red then move arm 10 to position x 169 y 150 z 100 else set signal tower 6 to blue and blinking, set arm 10 to controlled stop. Note that the two commands after the else are all within the else branch of the if then else block. A green arrow and text reading Optical detects red true points from the If then to the move to position block. A red arrow with text reading optical detects red false points from the if then to the set signal tower block at the beginning of the else branch.

After running either the if branch or the else branch, the project will move to the next block in the stack. The project flow of If then else blocks can also be shown to make the branches more visible.

Same project flow as before, but with the if branch and the else branch separated for clarity. On the left is red text reading red not detected false else with an arrow pointing to the set signal tower and set controlled stop blocks. On the right is the text red detected true then with an arrow pointing to the move to position block. Both the left and the right have orange arrows below indicating they would continue with the project below the if then else block.

Note that in these examples, the else branch is used to handle the condition of an object being neither red nor green. In that case, human intervention would be needed. Previously you learned that the blue light on the Signal Tower is used to indicate human intervention needed. Additionally, a controlled stop will stop the motion of the 6-Axis Arm in order to keep people and machinery safe. A Set signal tower block and a Set control stop block can be added to the else branch, so that an object that was not red or not green would trigger these safety features.

Contents of the else branch of the If then else block called out with a red box. This includes the set signal tower block and the arm controlled stop block.

The images so far have only shown how to handle the condition of the object being red. Another If then else is needed to check the condition of the object being green.

Same vexcode project with a second if then else block added directly after the first. This block and the blocks contained within read: if optical 5 detects green then move arm 10 to position x 172 y 1 z 100 else set signal tower 6 to blue and blinking, set arm 10 to control stopped.

Let's think about the project flow with this structure. Each time the project is run, multiple branches will be run, no matter what color is detected by the Optical Sensor.

Project flow image for two if then else blocks. The top half shows the same project flow from earlier, then an orange arrow can be seen moving from the first if then else block to the second. The second if then else block has green arrows and text to the side that reads: optical detects green true. The arrow points from the if then to the move to position block within the if then branch. Starting from the same point is red text and a red arrow that reads: Optical detects green false. The arrow points from the if then to the set signal tower block in the else branch.

Let's think about this with a red Disk as an example. If the Optical Sensor detects red, the condition of the first If then else block would report as true. The block inside of the if branch would run, moving the 6-Axis Arm over the red pallet. However, the condition inside of the second If then else block would report as false. This means the block inside of its else branch would run, putting the 6-Axis Arm into a controlled stop and flashing blue on the Signal Tower. Because the 6-Axis Arm would be in a controlled stop state, no other blocks in the project could run, and that would cease all operations until it was resolved. 

Project flow walking through the example of the optical sensor detecting red. The notes and arrows on the side of the project read optical detects red true, then the move to position block in the first if then branch will run. Text below that reads: else branch will not run because the if then branch was run. An arrow points to the second if then else block. Text beside that reads: optical detects green false and points the red arrow to the else branch of the second if then else block. an orange arrow points from the set arm to control stop block to the When arm 10 control stopped hat block to the side. Text below reads control stop will be triggered stopping all blocks except those attached to the when control stopped hat block.

While we can now account for an unexpected object (like a blue Cube), but the 6-Axis Arm will never reach the point in the project of releasing the Disk onto the pallet. Instead of checking the red and green conditions one after the other, there is a way to check if an object is green only if the object is not red.   

Nested If then else blocks 

Each of the branches of the If then else block can accept other C blocks within them. This is called 'nesting'. This means that the second If then else block can be nested within the else branch of the first, like this.Same project as above, but with the second if then else block inside of the else branch of the first if then else block. A red box is around the second if then else nested in the else branch of the first if then else block.

Now the condition of the Optical Sensor detecting green will only be checked when the previous condition (Optical Sensor detects red) reports as false. So when the Optical Sensor detects red, only the first if branch would run, moving the Disk above the red pallet. 

Close up view of the project with the move to position block in the if then branch of the first if then else block called out with a red box.

If the Optical Sensor detects green, the condition of the first if branch (Optical detects red) would report false, and so the else branch would run. The condition of the nested If then else block would be checked to see if the Optical Sensor detects green. That condition would report as true, so the block inside the second if branch would run, moving the Disk over the green pallet. 

If neither red nor green were detected by the Optical Sensor (like the case of a blue Cube being picked up), both of the conditions would report as false, and so the else branch of the nested If then else block would run, triggering a controlled stop and alerting workers to the need for human intervention. These project flows can also be visualized as a flow chart, like the one seen here.

Diagram explaining the flow of a project. At the top is the question optical detects red? A green line branches to the left and says move above the red pallet, then an orange line comes down from there to say place disk on the correct pallet. A red line branches from the original question and says false. This is followed by another question: optical detects green? Again the green line to the right says true and move above the green pallet. This connects with an orange line to the same box as the other green line that says place disk on the correct pallet. To the right is the red line that says false and set signal tower to flashing blue and trigger controlled stop.

Nested If then else blocks can be viewed as a cascading list of questions. Here the questions are as follows: 

  1. Is the object red? 
    1. Yes? Move over the red pallet and stop asking questions.
    2. No? Ask another question. 
      1. Is the object green?
        1. Yes? Move over the green pallet and stop asking questions.
        2. No? Then the object is not red or green. Change the Signal Tower colors and trigger a controlled stop.

Notice that the questions are only asked if the previous question is answered negatively. 

While this handles all of the conditions we want to check, there is another block that will check the conditions in the same way, but is easier to build than nesting blocks. 

Else if Block

The Else if block enables you to check multiple conditions within the same block. Each of the conditions in this block will be checked sequentially, only if the preceding condition reports as false. If all the Boolean conditions for the else if branches are false, the blocks inside of else section will be run.

Else if block in VEXcode. This block reads if then else if then else and has a plus icon to expand and add additional if then else segments to the block.

This block has the same project flow as the nested If then else blocks, but is easier to build because there is no need to nest blocks. You will learn more about coding with the Else if block in the following Lesson.

Check Your Understanding

Before moving on to the next Lesson, ensure that you understand the concepts in this Lesson by answering the following questions in your engineering notebook.

Check Your Understanding questions > (Google Doc / .docx / .pdf )


Select Next > to apply the Else if block in a project.