Skip to main content

Lesson 2: Post-Project Summary

The [If then else] block is used so the VR Robot can make a decision. If the Boolean condition is TRUE, then the blocks inside of the ‘If Then’ branch will run. If the Boolean Condition is FALSE, then the blocks inside of the ‘Else’ branch will run.

A diagram indicating the project flow of the if then else block within the forever loop. When the project is started the condition of the Distance Sensor is checked. If an object is found, that condition is true, then the Drive forward block executes. If No object is found, that condition reports false, else the turn right block executes. All of this is repeated continually within the Forever loop.

In this project, the VR Robot drove towards a building when the Distance Sensor detected an object and the <Distance found object> block reported as TRUE. This instructed the ‘If Then’ branch of the project to run. This branch includes the [Drive] block. The VR Robot then skips the ‘Else’ branch of the project.

The project is to the left with a green highlight showing the project flow of the if then branch executing. To the right the top down view of the Playground is shown, with an arrow and red box highlighting the castle, or object, detected by the Distance Sensor in this instance.

Because the [If then else] block is placed inside of a [Forever] block, the project loops back to the top of the [Forever] block to check the condition of the [If then else] block. This loop will continue forever, instructing the VR Robot to continually check the [If then else] condition hundreds of times per second.

Note that the [Drive] and [Turn] blocks inside the [If then else] block are non-waiting blocks. Non-waiting blocks do not stop the flow of a project while they are running.  A non-waiting behavior that is executed in an [If then else] block (such as drive forward if object is detected) will continue while the condition is checked, and will only change when the condition changes.

The same VEXcode VR project with arrows moving from the bottom of the Forever block back up to the top to indicated the project flow back to the top of the Forever loop to repeat all of the commands within it.

If the <Distance found object> block reports as FALSE, the ‘Else’ branch of the project is executed. The ‘Else’ branch includes the [Turn] block.

The project is to the left with a red highlight showing the project flow of the else branch executing. To the right the top down view of the Playground is shown, with curved arrows around the VR Robot indicating turning to detect an object, as there is not one directly in front of the Distance Sensor.

For Your Information

[If then else] blocks are used to ensure that only one branch in the project is run when the condition is checked. Only one instance or condition can be true at any time. This makes the project more efficient.

A diagram indicating the project flow of the if then else block within the forever loop. When the project is started the condition of the Distance Sensor is checked. If an object is found, that condition is true, then the Drive forward block executes. If No object is found, that condition reports false, else the turn right block executes. All of this is repeated continually within the Forever loop.

Compare the current project with the project created in Unit 7.  The Unit 7 project uses four [If then] blocks.

A diagram of the project flow of a sample project from Unit 7 with four If then blocks in sequence. In each case, the If parameter is using the Front Eye Sensor to detect a specific color, and perform a behavior based on the color detected. If the color is detected, that If then is true, and the block within it will run, and this will happen repeatedly for each section of the code.

When running through a project with multiple [If then] blocks, it could be possible that multiple conditions would report TRUE and the project runs through the commands in the [Forever] block. In this project, both conditions may report TRUE during the same [Forever] loop. Conflicts like this could cause the project to stop running or run more slowly.

A diagram of the project flow of two sequenced If then blocks within a forever loop. When started, the project will first check condition 1, the parameter of the first if then block. If that is true, the turn right for 90 block within it will run. If that condition is false, the project moves to the next if then block to check condition 2. If condition 2 is true, the drive forward for 200mm block within it will run. If it is false, the project will go back to the top of the forever loop to repeat the process again.

Using an [If then else] block would avoid bugs like this because only one condition can be TRUE at a given moment with this block.

A diagram of the project flow using an if then else block within a forever loop. When started, the condition of the if then branch will be checked. If it is true, and drive is moving, then the block within the If branch will run, and the robot will turn left 90 degrees; else, the robot will drive forward 200mm. This decision to check one condition will continually be made as the project is within a forever loop.

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf