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.

Post Project summary

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.

callout drive forward

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.

Post Project summary

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

else branch

Using Switch Blocks 

The image below displays this lesson's workflow using Switch blocks. 

wrapper

The image below shows the same behaviors converted into one Switch block with Python commands. 

while true

while True: begins the Forever loop. Because the condition is set to always be true, the indented behaviors will always repeat. Notice that this loop ends with a colon (:). The line, wait(5, MSEC) is indented beneath this command because the forever loop will repeat every 5 milliseconds. 

if drivetrain.is_moving(): is the "if branch." This line is indented at a default of 4 spaces because this behavior is nested within the repeat loop. Note that this command ends with a colon (:) to introduce the dependent behavior.

drivetrain.turn_for(LEFT, 90, DEGREES) is indented beneath that command, as this command is dependent upon whether the condition (in this case, the drivetrain moving) is met.  

else: is the "else branch." This Python command is indented under the while True command because it is part of the Forever loop that will always repeat.

drivetrain.drive_for(FORWARD, 200, MM) is indented at a default of 4 spaces under the else branch because this behavior is dependent upon whether the condition (in this case, the drivetrain not moving) is met. 

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.

P12

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

Project Flow

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.

p16

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.

p17

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf