More Decisions with the Finch (Scratch)
In the last lesson, you learned to create Boolean blocks with the Finch sensors. You used these blocks in the if, if else, and wait until blocks. In this lesson, you will learn to use one more decision block and to write programs that make more complicated decisions. As your programs become more complex, remember that you will need to spend time carefully planning what a program should do and how it should do it.
You may have used a repeat block to repeat a series of blocks some number of times. Scratch also includes the repeat until block. This block contains a space for a Boolean block. The actions inside the repeat until block will be repeated until the Boolean block is true. The repeat until block is a loop, just like the forever block. Each time through the loop, the program checks the Boolean block. If the block is false, the program repeats the blocks inside the loop. If the block is true, the program skips the blocks inside the loop and moves on to the next statement in the program.
Exercise: The example program shown below will make the Finch move forward and back until you pressed the ‘q’ key. Then the Finch will stop. How is this program different from a program that uses a forever loop? What happens if you quickly press and release the ‘q’ key while the robot is moving forward? Explain why this occurs.
Exercise: Modify one of your programs from the last lesson to use a repeat until loop instead of a forever loop. Describe one advantage of using a repeat until loop.
So far, you have used comparison operators (>, <, and =) to create Boolean blocks. Sometimes, you want to make more complicated decisions. For example, if you want the robot to stop when it reaches an obstacle, you probably want to check both the right and the left obstacle sensors. You can use the logic operators and, or, and not to make complex decisions. Each of these operators is a Boolean block that contains space for one or two other Boolean blocks.
The not block transforms the value of a Boolean block into its opposite. For example, if Finch left obstacle is true, then the value of the block below is false.
Exercise: Make the Finch beep while it is level. When the Finch is not level, it should stop beeping.
The and block is true only when both Boolean blocks inside it are true. If either Boolean block is false, then the and block is false. For example, the block below is true only if both light sensors detect that it is dark.
The or block is true if either of the Boolean blocks inside it is true. The or block is also true if both Boolean blocks inside it are true. The or block is only false if both Boolean blocks inside it are false. The block below is true if either light sensor detects that it is dark.
Exercise: Write a program that makes the Finch move forward if there is nothing in its way. If the robot detects an obstacle, it should move backward. The robot should stop when you press the ‘q’ key.
The logic operators can be used to program the Finch for more complex behaviors. You have also created complex behaviors by placing if else blocks inside loops. This is called nesting. You can take nesting even further.
Exercise: Look at the code below, and make a hypothesis about what the robot will do when this program runs. Then run the code to test your hypothesis. What other situations might require several nested if else blocks?