Page 186 - Computer science 868 Class 12
P. 186

Input:
              ‘+’

              Output:

              Addition Operator

                      Note: To input character value, we should use single quotation mark, i.e., ‘ ’. We cannot use real and
                      boolean data types as choice variable in the switch expression.


              Example 2: // Using int variable
                  int choice = sc.nextInt();
                  switch(choice)
                  {
                      case 1: System.out.println("Good Morning"):
                          break;
                      case 2: System.out.println("Good Afternoon");
                          break;
                      case 3: System.out.println("Good Evening");
                          break;
                      default: System.out.println("Good Night");
                  }

              Example 3: // Using String variable
                  class operator
                  {
                      public static void main(String name)
                      {
                           name = name.toLowerCase;
                          switch(name)
                           {

                             case "plus": System.out.println("+");
                                 break;
                             case "minus": System.out.println("-");
                                 break;
                             case "multiply": System.out.println("*");
                                 break;
                             case "division": System.out.println("/");
                                 break;
                             default: System.out.println("No match found");
                          }
                      }
                  }
              Input:

              “plus”

              Output:

              +
              Sometimes, there may be a situation in a program where we may not require to exit after one case is executed and go
              to the next case. This situation where the control moves from one case to another in the absence of “break” statement
              is known as Fall Through.



                184184  Touchpad Computer Science-XII
   181   182   183   184   185   186   187   188   189   190   191