Java branching switch statement and programming example

 

 Java branching switch statement

·         Syntax of switch statement

 Switch (value)  {

case: 1: (value)

//statement

  break;

case: 2: (value)

//statement

  break;

default:

}

Q.1 write a java program which will print of the name of the month on given input the month on digit or number?

Program:-

  class Month    {

                                              public static void main (String  args[])  {

                                                 int  x=12;

                                                switch  (x)     {

                                                  case 1:

                                                   System.out.println(“january”);

                                                   break;

                                                   case 2:

                                                   System.out.println(“february”);

                                                   break;

                                                  case 3:

                                                   System.out.println(“march”);

                                                   break;

                                                  case 4:

                                                   System.out.println(“april”);

                                                   break;

                                                   case 5:

                                                   System.out.println(“may”);

                                                   break;

                                                    case 6:

                                                   System.out.println(“june”);

                                                   break;

                                                 case 7:

                                                   System.out.println(“july”);

                                                   break;

                                                 case 8:

                                                   System.out.println(“august”);

                                                   break;             

                                                   case 9:

                                                   System.out.println(“september”);

                                                   break;

                                                   case 10:

                                                   System.out.println(“october”);

                                                   break;

                                                 case 11:

                                                   System.out.println(“november”);

                                                   break;

                                                   case 12:

                                                   System.out.println(“december”);

                                                   break;

                                                  default:

                                                   System.out.println(“this is not a valid number”);

                                                   break;

                                                   }

                                      }

                       }

 

 


 

Post a Comment

0 Comments