Page 83 - Viva ICSE Computer Studies 8 : E-book
P. 83

Syntax:
                     If(test condition)
                      {
                                                                                                              Start
                      Statements
                      }

                     ElseIf(test condition)                                   False      Test       False     Test
                                                                                      condition             condition
                      {                                                                   2                     1

                      Statements
                      }                                                                      True                  True

                     ElseIf(test condition)                                          Body of else if        Body of if
                      {
                                                                       Body of else
                      Statements

                      }                                                                                       Stop
                     Else

                      {
                      Statements

                      }




                    Example:  Create a program that will
                    display a message.

                      (a)  ‘Distinction’, if a particular value is
                           greater than or equal to 70.

                      (b)  ‘First class’, if a particular value is
                           greater than or equal to 60.

                      (c)  ‘Second class’, if a particular value
                           is greater than or equal to 50.

                      (d)  ‘Fail’, if no match is found.



                    Switch Statement


                    The Switch statement works as a jumping statement where the control is transferred to a
                    specifi c case as per the given switch value. Each case ends with a break statement which acts
                    as a terminator and the control comes out of block. It also includes a default statement which
                    is executed only when the switch value doesn’t match the cases.

                    Let’s understand the Switch statement with the syntax and an example.




                                                                                                                    71
   78   79   80   81   82   83   84   85   86   87   88