Page 146 - Learn To Program With Scratch
P. 146

In many programming situations, however, you may want to alter this
                         sequential flow of program execution. If you were writing an application to
                         tutor children in basic arithmetic, you’d want to execute certain blocks to
                         reward correct answers and a completely different set of blocks for wrong
                         answers (to reveal the right answer or offer another chance, for example).
                         Your script can decide what to do next by comparing the student’s input
                         with the correct answer. This is the basis of all decision-making tasks.
                             In this chapter, we’ll explore the decision-making commands available
                         in Scratch and write several programs that use these commands to test
                         inputs and perform different actions.
                             First, I’ll introduce you to Scratch’s comparison operators and show
                         how you can use them to compare numbers, letters, and strings. Then, I’ll
                         introduce the if and if/else blocks and explain their key role in decision
                         making. You’ll also learn how to test multiple conditions using nested if and
                         if/else blocks and write a menu-driven program to put these blocks into
                         action. After that, I’ll introduce logical operators as an alternative way to
                         test multiple conditions. In the last section, we’ll write several interesting
                         programs based on all of the concepts you’ve learned so far.


              comparison operators

                         You make decisions every day, and each decision normally leads you to per-
                         form certain actions. You may think, for example, “If that car is less than
                         $2,000, I’ll buy it.” You then ask about the car’s price and decide whether
                         or not you want to buy it.
                             You can make decisions in Scratch, too. Using comparison operators,
                         you can compare the values of two variables or expressions to determine
                         whether one is greater than, less than, or equal to the other. Comparison
                         operators are also called relational operators because they test the relation-
                         ship between two values. The three relational operators supported in
                         Scratch are shown in Table 6-1.

                         Table 6-1: Relational Operators in Scratch
                          Operator          Meaning           Example

                                            greater than
                                                              Is price greater than 2,000?

                                            less than
                                                              Is price less than 2,000?

                                            equal to
                                                              Is price equal to 2,000?







           124   Chapter 6
                                           www.it-ebooks.info
   141   142   143   144   145   146   147   148   149   150   151