Nested switch statement in JAVA explained!!!

Nested switch statement in JAVA 

You can use switch as a part of statement sequence at an outer switch. This is called a nested switch. since a switch statement defines its own block, no conflict arise between the case constant in the inner switch and these in the outer switch

Nested-Switch statements refers to Switch statements inside of another Switch Statements.

Syntax :-

switch (expression)
{
    case constant expression:
     {
          switch(expression)
           {
                case constant expression:
                {
                     statement;
                     break;
                 }
                 default :
                 {
                      statement;
                  }
            }
       default:
       {
              statement;
       }
    }
}
      

ex :-

class test
{
    public static void main(String args[])
    {
        int i=0;
        int j=1;
        switch(i)
        {
           case 0:
           {
              switch(j)
             {
                  case 0:
                  {
                        System.out.println("i is 0 & j is 1 ");
                        break;
                   }
                 case 1:
                 {
                       System.out.println("i is 0 & j is 1 ");
                       break;
                 }
                default:
                {
                    System.out.println("invalide nested default case ");
                }
          break;
         }
         case 1:
         {
               System.out.println("i is 1");
               break;
          }
         default:
         {
               System.out.println("no matching form ");
          }
     }
  }
}
   
             



Post a Comment

0 Comments