Page 147 - Learn To Program With Scratch
P. 147

BooLeanS in the reaL worLD
                             The word Boolean is used in honor of George Boole, a 19th-century British
                             mathematician who invented a system of logic based on just two values:
                             1 and 0 (or True and False) . Boolean algebra eventually became the basis
                             for modern-day computer science .
                                 In real life, we use Boolean expressions all the time to make decisions .
                             Computers also use them to determine which branch of a program to fol-
                             low . A robotic arm may be programmed to inspect moving parts on an
                             assem bly line and move each part to Bin 1 if goodQuality = true, or Bin 2 if
                             goodQuality = false . Home security systems are usually programmed to sound
                             an alarm if the wrong code is entered (correctCode = false) or deactivate when
                             we enter the correct code (correctCode = true) . A remote server may grant
                             or deny access when you swipe your credit card at a department store based
                             on whether your card was valid (true) or invalid (false) . One computer in your
                             vehicle will automatically deploy the air airbags when it decides that a collision
                             has occurred (collision = true) . Your cell phone may display a warning icon
                             when the battery is low (batteryLow = true) and remove the icon when the bat-
                             tery’s charge is acceptable (batteryLow = false) .
                                 These are just few examples of how computers cause different actions to
                             be taken by checking the results of Boolean conditions .



                             Note that the blocks in Table 6-1 all have a hexagonal shape. As you
                          might recall from Chapter 5, that means the result of evaluating one of
                          these blocks is a Boolean value, which can be either true or false. For this
                          reason, these expressions are also called Boolean expressions.
                             For example, the expression price < 2000 tests whether the value of the
                          variable price is less than 2,000. If price is less than 2,000, the block returns
                          (or evaluates to) true; otherwise, it returns false. You can use this expres-
                          sion to construct your decision condition in the form, “If (price < 2000),
                          then buy the car.”
                             Before we look at the if block, which allows you to implement such a
                          test, let’s go over a simple example that illustrates how Boolean expressions
                          are evaluated in Scratch.

                          Evaluating Boolean Expressions
                          Let’s say that we set two variables, x and y, as follows: x = 5, and y = 10.
                          Table 6-2 shows some examples that use Scratch’s relational blocks.
                             These examples reveal several important points about relational opera-
                          tors. First, we can use them to compare both individual variables (such as
                          x, y) and complete expressions (such as 2 * x and x + 6). Second, the result
                          of a comparison is always true or false (that is, a Boolean value). Third, the
                          x = y block doesn’t mean “Set x equal to y.” Instead, it asks, “Is x equal to y?”
                          So when the statement set z to (x = y) is executed, the value of x is still 5.


                                                                              Making Decisions   125

                                           www.it-ebooks.info
   142   143   144   145   146   147   148   149   150   151   152