Lesson 4: Using [Forever] blocks
Repeatedly Checking Conditions
As observed in the previous Lesson, the [If then] block only checks conditions once. In order for conditions contained in the [If then] blocks to be checked repeatedly, a [Forever] C block is needed. The [Forever] block is a C block from the Control category that repeats the behaviors contained inside of it, forever.
-
Load the Unit7Lesson3 project from the previous Lesson.
-
Rename the project Unit7Lesson4.
-
Drag a [Forever] block into the workspace and attach it to the top of the stack of blocks. Notice that this will include all four [If then] blocks inside of the [Forever] block.
For Your Information
Blocks can be added to the beginning or middle of the project stack by lining up the top of the block, with the desired placement in the stack.
This video below shows how to add the [Forever block] around the four [If then] blocks in the current project. Notice that the top of the [Forever] block is lined up just after [When started] and the first [If then] block. The [Forever] block then wraps around all the following blocks in the project stack.
- Now that the [Forever] block is added to the project so that all of the [If then] blocks will constantly have their conditions checked, open the Disk Maze Playground and run the project.
-
When this project is run, the VR Robot will navigate the Disk Maze Playground from start to finish. Once the VR Robot detects red, it will stop driving.
-
In the project from the previous Lesson, the conditions within the [If then] blocks were only checked once. Since the conditions within the [If then] blocks were only checked once, the VR Robot drove forward forever because the condition of no color being detected was reported as TRUE.
-
Now that the [Forever] block has been added, each condition of the [If then] C blocks will be checked repeatedly. During the flow of the project, if the condition of the [If then] C block is TRUE, the blocks inside of the [If then] C block are executed. If the condition of the [If then] C block is FALSE, the blocks inside of the [If then] C block are skipped, and the flow of the project will continue to the next block in the stack. This enables the VR Robot to perform discrete behaviors such as turning or stopping once it detects a certain color.
-
[If then] blocks are used with [Forever] blocks to ensure that conditions are constantly checked.
Using Switch Blocks
This is the Switch [Forever] block. The Switch Python command for Forever creates a while loop where the condition is True. The commands nested within the loop when then be repeated forever, as the condition is always True.