Page 92 - PortGPT_V2.1_C8_Flipbook
P. 92

Switch Statement


                  Due to use of multiple if statements, the program becomes more complex and lengthy. To avoid such
                  problem, JavaScript provides switch statement. The switch statement executes certain statements
                  depending on user's choice. The case statement is used to define different cases where each case has
                  a unique label and a set of statements associated with it.
                  The syntax of the switch statement is given the following:

                  Syntax:

                  switch(choice)
                  {

                      case 1:
                        Statements of block 1;
                        break;
                      case 2:

                        Statements of block 2;
                        break;
                  .
                  .

                  .
                      default:
                        Statements of block last;
                  }



                                                              True
                                            Test Expression 1        Statement 1


                                                    False


                                                              True
                                            Test Expression 2        Statement 2


                                                    False


                                                              True
                                            Test Expression 3        Statement 3


                                                    False            Body of else



                                                                            Statement just below
                                                                              if...elif...else ladder




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