Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Sunday, 26 June 2011

Nested switch statement in java


Nested switch:
                         We can use a switch as part of the statement sequence of an outer switch. This is called nested switch. We know that each switch defines its own block of code but case constants of outer switch have no conflicts with the inner switch's case constants.

General syntax:
                           switch (value)
                           {
                            case 1:
                               switch (value)
                               {
                                case 1:
                                statement(s)
                                break;
                                .
                                .
                                case n:
                                statement (n)
                                break;
                                default:
                                statement
                                }
                          case 2:
                          statement(s)
                          break;
                          .
                          .
                          case n:
                          statement (n)
                          break;
                          default:
                          statement
                         }    
Here, value can be a variable or any constant value. Remember case 1 of outer switch have no conflicts with case 1 of inner switch. If the value matches with case 1 (outer switch's) then inner switch will be executed otherwise it will be bypassed.


No comments:

Post a Comment