Page 164 - Computer
P. 164

Computer Science   Input N                   or more paths. Once the conditional
                           Start
                                                   execution  is  finished,  it  will  re-join
                                                   before  leaving the  structure.  Selection
                                                   control structure uses ‘IF…THEN’
                                                   or ‘IF….  THEN ...ELSE’ or a CASE
        FOR ONLINE READING ONLY
                                                   structure in pseudocodes and flowcharts.
                         Input M
                                                   IF … THEN
                                                   IF…THEN,  the structure executes the
                      Sum = N + M
                                                   process if the condition is true. If it is not
                                                   true, it skips the process. The diamond-
                       Output Sum                  shaped box in flowcharts represents the
                                                   decision or condition.  The syntax for
                           END                     IF…THEN is:
                                                          IF (Condition) THEN
           Figure 8.8: Example of sequential control
                              structure                               Statement

          Selection   control    structure    in          END IF;
          flowcharts                               The corresponding flowchart is

          The  selection  control  structure  is used   represented as shown in Figure 8.9.
          to make decisions on an algorithm.
          A condition  is a logical  expression or
          statement that can be assessed as true or            YES           Condition
          false. The ability to control flow of the
          algorithm  by making decisions based
          on conditions is provided by selection
          control structures. It enables choosing   Statement(s)                NO
          between  several  paths  of  execution.
          Examples from real life include deciding
          what to wear based on the weather. You
          dress in a T-shirt and shorts when it is

          sunny, but you take an umbrella and a
          raincoat when it is raining.               Figure 8.9: Example of IF-THEN selection

                                                   Example of IF…THEN selection
          Under the  selection  control structure,   control structure
          one condition is evaluated first and then
          the program flows into one of the two    Write  an  algorithm  using  a  flowchart
                                                   for the headmaster of Chato secondary

                                               156
                                                                  for Secondary Schools
   159   160   161   162   163   164   165   166   167   168   169