⭐ 1.5.5 Coding Logically - Boolean Operator Blocks

View

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.

  1. Drag a “() > ()” block from the Operators Block Palette to the Scripts Area.
  1. Enter 3 into the left side of the () > () block and 2 into the right side.   
  1. 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.      
  1. Right-click the () > () in the Scripts Area. In addition to the normal right-click options, you also see three more: <, =, and > (F.
  1. Select the less than (<) symbol from the right-click menu.  The () > () block changes into a () < () block and leaves your values as they were.
  1. 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.

  1. Drag a () and () block from the Operators Block Palette to the Scripts Area.
  1. Snap your (3) < (2) block into the first space within the () and () block.
  1. Drag a () > () block from the Operators Block Palette to the Scripts Area and snap it into the right side of the () and () block.              
  1. Change the values in the () > () block to 99 and 1.
  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. 

  1. Drag a () or () block from the Operators Block Palette to the Scripts Area.
  1. Drag the (3) < (2) block from the left side of the () and () block to the left side of the () or () block.
  1. Drag the (99) > (1) block from the right side of the () and () block to the right side of the () or () block.       
  1. 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.         

 


Last modified: Thursday, 23 September 2021, 12:11 PM