Skip to main content

Lesson 4: Coding with the Else If Block

Lesson 4: Coding with the Else If Block

In the previous Lesson, you learned about the difference between If thenIf then else, and Else if blocks. In this Lesson, you will learn how to apply Else if blocks in a VEXcode project. You will learn about: 

  • Coding with the Else if block.

By the end of the Lesson, you will be ready to apply your learning to sort and stack Disks in the Putting It All Together Activity.This Unit's build with a red disk on the right pallet and a green disk on the left.

Building the Project with the Else if Block

As discussed previously, the Else if block enables you to check multiple conditions within the same block.

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.

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.

Let's build the project to sort Disks by color to make it easier to understand the block.

Open your Unit 3 Lesson 2 Activity project in VEXcode. Rename it to Unit 3 Lesson 3 and save it to your device.

VEXcode EXP toolbar with a callout on the project name box. The project name reads Unit 3 Lesson 3.

Remove both of the If then blocks from your project. Right-click on each block and select Delete block from the Context Menu, as shown in the video here. 

Video file

Add an Else if block below the Move the Disk to the correct pallet comment. 

The video here shows the Else if block being dragged from the Toolbox into the project in the same space where the two If then blocks were previously located.

Video file

Add the Optical detects color block into the hexagonal space in the if branch of the block, as shown in the video here. Be sure the parameter is set to red.

Video file

Add a Move to position block inside the if branch of the C block, as shown in the video here. Set the parameters to the x, y-coordinates of the position over the red pallet. Leave the z-coordinate at 100, so that the 6-Axis Arm is positioned above the pallet.

Be sure to use the coordinates you recorded in your engineering notebook, as those shown in the example may differ from your own.

Video file

Add an Optical detects color block to the hexagonal space in the else if branch of the block, as shown in the video here. Set the parameter to green

Video file

Add a Move to position block inside the else if branch of the C block, as shown in the video here. Set the x and y-parameters to the coordinates of the position over the green pallet. Leave the z-parameter at 100, so that the 6-Axis Arm is positioned above the pallet.

Be sure to use the coordinates you recorded in your engineering notebook, as those shown in the example may differ from your own.

Video file

Add a Set signal tower block inside the else branch of the C block, as shown in the video here. Set the parameters to blue and blinking using the second and third parameter dropdowns. 

This will signal the need for human intervention, should neither of the above conditions be met.

Video file

Add an Enable 6-Axis Arm Control Stop block beneath the Set signal tower block as shown in the video here. This will trigger the controlled stop behaviors in the event of an object that is not green or red.

Video file

Read the code and predict what the 6-Axis Arm will do when the project is run. 

Record your prediction in your engineering notebook.

Full VEXcode EXP project built using the above instructions. Below the when started block, the project reads as follows: set optical 5 light on, a comment stating pick up disk from the loading zone, set arm 10 end effector to magnet, move arm 10 to position x 96 y 254 z 18 millimeters, set arm 10 magnet to engaged, a comment stating move the disk over the optical sensor to detect the color, move arm 10 to position x 49 y 177 z 70 millimeters, a comment stating move the disk to the correct pallet, if optical detects red then move arm 10 to position z 169 y 150 z 100 millimeters else if optical 5 detects green then move arm 10 to position x 172 y 1 z 100 else set signal tower to blue and blinking, set arm 10 to control stop, a comment stating place disk on the pallet, increment arm 10 position by x 0 y 0 z negative 50 millimeters, set arm magnet to released, increment arm 10 position by x 0 y 0 z 50.

Place a red Disk on the loading zone at Brain Tile location 19. Be sure the Brain is connected to VEXcode, and download the project.

Top right of the VEXcode toolbar with a callout on the Download button. This is the third button from the top left and is between the EXP Brain icon and Run button.

Run the project to test it. 

Observe the behavior of the 6-Axis Arm as the project runs. 

EXP Brain with the Unit 3 Lesson 3 project title on the screen and the Run option highlighted. A red box is on the checkmark button on the Brain.

Stop the project when the 6-Axis Arm has finished moving. Record your observations in your engineering notebook.

Did the 6-Axis Arm move the red Disk as you predicted? Why or why not? 

Run the project a second time, with a green Disk placed in the loading zone. Does it sort the Disk as intended? Why or why not?

Project running on the Brain with the x button highlighted to stop the project.

For Your Information

You can also test the project using either an empty loading zone, or a Cube, in order to observe the behavior of the 6-Axis Arm when neither red nor green is detected. Note that when the controlled stop is triggered, the project needs to be stopped before you can proceed. 

In this video, you can see the 6-Axis Arm move to pick up a blue Cube and place it over the Optical Sensor. Once detected, the 6-Axis Arm stops moving and the Signal Tower begins blinking blue. The Signal Tower continues to blink blue until someone stops the project on the Brain, then all lights stop.

Video file

For Your Information

The Else if block can be expanded to include additional conditions within the same block. To add another else if condition, click on the + on the else line. This will add a new else if condition to the bottom of the current list. To remove an else if condition, click on the on the else line. This will remove the last else if condition line, but will not delete the blocks in the condition.

This video here shows two additional else if conditions being added with the + button, then removed with the - button. 

Video file

Reviewing the Project

Let's review the project flow with the Else if block. This project follows the same logic as the nested If then else blocks.

If red is detected by the Optical Sensor, the condition of the if branch reports as true, and the block inside that branch will run. The red Disk will be moved over the red pallet. The project then exits the Else if block, moving on to the next block in the stack, and releasing the Disk onto the pallet. Else if block from the project that was run. A green arrow is to the side starting from the first If then condition. Green text with the arrow reads optical detects red, True. The arrow then points to the move to position block within the first if then branch. A red arrow also starts from the same if then point. Text reads optical detects red, False. The red arrow points to the second if then condition where the condition of optical detects green is checked.

If green is detected by the Optical Sensor, the condition of if branch reports as false, so the condition of the else if branch is checked. The condition of the else if branch reports as true, so the block inside of that branch will run. The green Disk will be moved over the green pallet. The project then exits the Else if block, moving on to the next block in the stack, and releasing the Disk onto the pallet. Project flow of the same code snippet focused on the first Else if branch where the previous project flow image ended. Starting from this point is a green arrow that reads Optical detects green, true. The green arrow points at the move to position block inside of this else if branch. The red arrow that starts from the same location points to the set signal tower block in the else branch and has text that reads Optical detects green, false.

If neither red nor green is detected by the Optical Sensor, both of the conditions report as false, so the blocks in the else branch will run. This would cause the Signal Tower to communicate that human intervention is needed, and trigger a controlled stop of the 6-Axis Arm. Because the controlled stop has been triggered, the project stops and does not move on to the next block in the stack.

Else if 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. This is the same pattern that happens in the Else if block.

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 move on to the Putting It All Together Activity.