Page 172 - CA_Blue( J )_Class9
P. 172

case 'D': System.out.println("Worker");
                                         break;
                              default: System.out.println(" Choose valid option ");
                         }
                                                   Start


                                                  Switch
                                           (Conditional Expression)



                                                   Case          true  Statement 1
                                                 Condition 1              break;

                                                 false


                                                   Case          true  Statement 2
                                                Condition 2               break;


                                                 false

                                                   Case          true  Statement n
                                                Condition n               break;

                                                 false


                                                                 true
                                                  Default                Default
                                                                        Statement

                                                                                 Statement just below
                                                                                     Switch Case



                                                                                        Stop

                  In the above example, if the code is 1, case A is executed. Then the break statement will send the control out of the
                  switch block. If the code is 2, then case B will be executed, and so on. If choice value is not satisfied, then default
                  will execute as the last statement.


                    Program 9     Write a program to input two numbers and print the sum or positive difference according to
                                  user’s choice.
                     1      import java.util.*;

                     2      class sum_difference
                     3      {

                     4          public static void main()
                     5          {





                   170    Touchpad Computer Applications-IX
   167   168   169   170   171   172   173   174   175   176   177