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

CONDITIONS


              There are situations in life when we need to decide based on a situation. For example, if it rains, we will stay
              at home, or else we will visit the market. Similar situations arise in programming as well, where we need to
              make some decisions and based on these decisions, execute a set of commands. We need to determine
              which action is to be taken and which statement is to be executed, if the outcome is true or false.

              Conditions in Programming

              Conditions are like questions that a computer asks to make a decision. They check if something is true
              or false. If the answer is true, the computer does one thing. If it is false, it can do something else or
              nothing at all. For example, if it is raining, you take an umbrella. If it is not raining, you don’t take one.
              In programming, conditions help the computer decide what to do next.

              Conditional statements, also known as decision-making, are used in programming to execute different
              blocks of code based on certain conditions. The following conditional blocks are available in Scratch:

                  If-then block

                  If-then-else block

              If-then block

              The If-then block in Scratch checks a condition. If the condition is true, it runs the blocks inside it. If the
              condition is false, it skips the blocks and does nothing.
              For example, to say 'Hello!' if spacebar key pressed while touching pointer, use the following script:


















              The above script shows  how  an if-then  block  creates conditional  behaviour  in  Scratch. When  the
              spacebar key is pressed, the script starts running and checks if the mouse pointer is touching the
              sprite. Only when both conditions are true will the sprite say "Hello!" for two seconds. The if-then block
              is a conditional statement that ensures the greeting happens only when the user presses spacebar key
              while pointing at the sprite.

              The                      block is an event block. Scripts placed underneath this block will activate when

              the specified key is pressed.

              If-then-else block

              The If-then-else block in Scratch checks a condition. If the condition is true, it runs the blocks inside the if
              part. If the condition is false, it runs the blocks inside the else part.



                  90  Premium Edition-VIII
   87   88   89   90   91   92   93   94   95   96   97