Page 53 - 6437
P. 53

printf("Excellent!\n" );

                                   break;

                                case 'B' :
                                case 'C' :
                                   printf("Well done\n" );

                                   break;

                                case 'D' :
                                   printf("You passed\n" );
                                   break;

                                case 'F' :
                                   printf("Better try again\n" );

                                   break;
                                default :

                                   printf("Invalid grade\n" );
                                }

                                printf("Your grade is  %c\n", grade );


                                return 0;
                            }

                          When the above code is compiled and executed, it produces the following result:

                 Well done

                 Your grade is B


                          Nested switch Statements
                          It is possible to have a switch as a part of the statement sequence of an outer switch. Even
                   if the case constants of the inner and outer switch contain common values, no conflicts will arise.

                          Syntax
                          The syntax for a nested switch statement is as follows:


                 switch(ch1) {
                    case 'A':

                        printf("This A is part of outer switch" );






                                                                                                      55
   48   49   50   51   52   53   54   55   56   57   58