Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Nested switch Statement



It is also possible to have a nested set of switch statements. The general form of the statement is shown below −

switch(expression) { 
   case expression #1: 
   statement #1 
   ... 
   case expression #2: 
   statement #2
   ... 
   case expression #N: 
   statement #N 
   ... 
   default: 
   statement #Default 
   ... 
}

Use of Nested Switch Statements for Integers

Following is an example of the nested switch statement −

Example.groovy

class Example { 
   static void main(String[] args) { 
      //Initializing 2 variables i and j 
      int i = 0; 
      int j = 1; 
		
      // First evaluating the value of variable i 
      switch(i) { 
         case 0: 
            // Next evaluating the value of variable j 
            switch(j) { 
               case 0: 
                  println("i is 0, j is 0"); 
                  break; 
               case 1: 
                  println("i is 0, j is 1"); 
                  break; 
               
               // The default condition for the inner switch statement 
               default: 
               println("nested default case!!"); 
            } 
         break; 
			
         // The default condition for the outer switch statement 
         default: 
            println("No matching case found!!"); 
      }
   }
}

Output

In the above example, we are first initializing two variables to 0 and 1 respectively. We then have a switch statement which evaluates the value of the variable i. Based on the value of the variable it will execute the relevant case containing nested set of statements. The output of the above code would be −

i is 0, j is 1

Use of Nested Switch Statements for Floats

Following is an example of the nested switch statement −

Example.groovy

class Example { 
   static void main(String[] args) { 
      //Initializing 2 variables i and j 
      float i = 0.0; 
      float j = 1.0; 
		
      // First evaluating the value of variable i 
      switch(i) { 
         case 0.0: 
            // Next evaluating the value of variable j 
            switch(j) { 
               case 0.0: 
                  println("i is 0, j is 0"); 
                  break; 
               case 1.0: 
                  println("i is 0, j is 1"); 
                  break; 
               
               // The default condition for the inner switch statement 
               default: 
               println("nested default case!!"); 
            } 
         break; 
			
         // The default condition for the outer switch statement 
         default: 
            println("No matching case found!!"); 
      }
   }
}

Output

In the above example, we are first initializing two float variables to 0.0 and 1.0 respectively. We then have a switch statement which evaluates the value of the variable i. Based on the value of the variable it will execute the relevant case containing nested set of statements. The output of the above code would be −

i is 0, j is 1

Use of Nested Switch Statements for Dynamic Variables

Following is an example of the nested switch statement −

Example.groovy

class Example { 
   static void main(String[] args) { 
      //Initializing 2 variables i and j 
      def i = 0.0; 
      def j = 1.0; 
		
      // First evaluating the value of variable i 
      switch(i) { 
         case 0.0: 
            // Next evaluating the value of variable j 
            switch(j) { 
               case 0.0: 
                  println("i is 0, j is 0"); 
                  break; 
               case 1.0: 
                  println("i is 0, j is 1"); 
                  break; 
               
               // The default condition for the inner switch statement 
               default: 
               println("nested default case!!"); 
            } 
         break; 
			
         // The default condition for the outer switch statement 
         default: 
            println("No matching case found!!"); 
      }
   }
}

Output

In the above example, we are first initializing two variables to 0.0 and 1.0 respectively. We then have a switch statement which evaluates the value of the variable i. Based on the value of the variable it will execute the relevant case containing nested set of statements. The output of the above code would be −

i is 0, j is 1
groovy_decision_making.htm
Advertisements