⭐ 1.5.5 Coding Logically - Boolean Operator Blocks
The Boolean operator blocks each use a value, or multiple values, to decide whether something is true or false. Boolean operating is also known as Boolean logic. To quickly review, the six Boolean blocks in the Operators Block Palette are:
● () > (). The greater than block produces a value of true if the first number is bigger than the second.
● () < (). The less than block produces a value of true if the first number is smaller than the second.
● () = (). The equal to block produces a value of true if the values (which may be numbers or text) on both sides of the equal sign are the same.
● () and (). The AND block produces a value of true if the statements on both sides of the AND are true.
● () or (). The OR block produces a value of true if either statement to the right or to the left of OR is true.
● Not (). The NOT block produces a value of true if the result of the block inside of it is false.
Boolean blocks are used inside control blocks to choose between multiple paths or to decide whether to keep looping.
In the next activity, you will use Boolean operator blocks to experiment with creating true and false values.
Activity: operations to produce true and false values.
The following activity shows how different operations produce true or false values.
- Drag a “() > ()” block from the Operators Block Palette to the Scripts Area.
- Enter 3 into the left side of the () > () block and 2 into the right side.
- Double-click the (3) > (2) block in the Scripts Area. A talk bubble appears next to the block and tells you that the result is true, that 3 is greater than 2.
- Right-click the () > () in the Scripts Area. In addition to the normal right-click options, you also see three more: <, =, and > (F.
- Select the less than (<) symbol from the right-click menu. The () > () block changes into a () < () block and leaves your values as they were.
- Double-click the () < () block. A talk box appears displaying false, showing that 3 is not less than 2.
|
|
Figure 32: Finding out whether the number 3 is smaller than the number 2 (left) and additional right-click options (right)
Using the () < (), () = (), and () > () blocks by themselves is simple, but you can also combine them to code much more complicated logic. Follow these steps to see some of the things that Scratch can do with logic.
- Drag a () and () block from the Operators Block Palette to the Scripts Area.
- Snap your (3) < (2) block into the first space within the () and () block.
- Drag a () > () block from the Operators Block Palette to the Scripts Area and snap it into the right side of the () and () block.
- Change the values in the () > () block to 99 and 1.
- Double-click the “() and ()” block. The result is false, as shown below, because only one of the two halves of the () and () block is true.
- Drag a () or () block from the Operators Block Palette to the Scripts Area.
- Drag the (3) < (2) block from the left side of the () and () block to the left side of the () or () block.
- Drag the (99) > (1) block from the right side of the () and () block to the right side of the () or () block.
- Double-click the () or () block. This block produces a true value because one of the sides of the block is true, as shown in Figure 1.35.