Page 110 - Computer Science V2.0 Class 11
P. 110

In general, execution of statements involving a condition may be represented in a flowchart as shown in Fig 4.6.


                                                   Start





                                                                False   Statement(s), when
                                                 Condition
                                                                         condition is false

                                                       True

                                             Statement(s), when
                                              condition is true




                                                    Stop

                                    Fig 4.6: Flowchart representation for branching based on some conditions
              Pseudocode involving a condition may appear in any of the following two forms:

               if  condition then                               if  condition then
                   Sequence of statements                           Sequence of statements


               else

                     Sequence of statements
                         Fig 4.7 (a) involves an else part          Fig 4.7 (b) Does not involve an else part

              Example: Write a pseudocode to check whether a number is odd or even.
              ● Input: An integer (say, N)
              ● Process: Check whether N is divisible by two. If so, declare it even, otherwise declare that the number is odd.
              ● Output: Display the message:
                  Even: if N is divisible by two

                  Odd: if N is not divisible by two
              Most programming languages provide the modulo operator (often denoted by the symbol %) which yields the remainder
              on division. Thus, 32%5 yields 2 because by dividing 32 by 5 we get the remainder of 2 (32 = 5x6+2). Similarly, the
              operators == and <> are used to test for the equality and inequality, respectively, of two numbers. Thus, 7==5 and
              7 <> 5 yield False and True, respectively. Having learned the operators %, and ==, it is straightforward to write
              pseudocode to check whether a number is odd or even.

                  input N

                  if N % 2 == 0 then
                          Print "Even"
                  else
                     Print "Odd"
              Note that the string to be output printed is placed within the quotes (maybe single quotes or double quotes). However,
              the computer will only print the text Even or Odd without any enclosing quotes.




                96   Touchpad Computer Science (Ver. 2.0)-XI
   105   106   107   108   109   110   111   112   113   114   115