CS Level 1 - VEXcode VR Blocks

Unit 9 - Developing Algorithms

# 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.

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.

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.

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

[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.

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

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.

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.