Page 93 - 2502_Pakistan-kifayat_C-8
P. 93

For example, to make a sprite say “Hello!” or “Bye!” after pressing spacebar key, use the following script:
























                 This script shows how an if-then-else block works in Scratch. When the spacebar key is pressed, the
                 script checks if the mouse pointer is touching the sprite. If it is, the sprite will say "Hello!" for two
                 seconds. If not, it will say "Bye!" for two seconds. This means the blocks inside the if run only when the
                 condition is true, and the blocks inside the else run when it is false. This kind of conditional logic helps
                 make projects interactive, so things happen only when the user wants them to.


                        Block               Description and function                           Example
                                       Checks a condition and runs blocks      If the sprite touches the edge, stop
                                       only if it is true.                     moving.





                                       Checks a condition and runs one set     If touching edge, turn; else, move
                                       of blocks if true, another if false.    forward.











                 Combining Different Types of Conditions

                 Sometimes, a program needs to check more than one condition at the same time. Combining conditions
                 lets the program decide what to do when multiple requirements are met. We use logical operators
                 blocks like and, or, and not to combine conditions inside blocks.

                 Combining conditions helps to create more precise and complex decisions, making programs smarter
                 and more interactive.

                 You can also use the following Operators blocks to combine different types of conditions:

                                     block: The block joins two Boolean blocks so they both have to be true to return
                   true. If they are both true, the block returns true; if only one is true or none are true, it returns false.




                                                                                     #Intermediate Level Programming  91
   88   89   90   91   92   93   94   95   96   97   98