Grails Cookbook - A collection of tutorials and examples

Groovy Switch Statement Examples

The Switch statement in Groovy is more powerful than in Java. There are things that are easy to do in Groovy switch statements that are not available in Java. It is also more concise and easier to read. Below are several examples on how to use Groovy switch statements.

A Simple Switch Statement

What is possible in Java switch statements are also valid in Groovy. Below is a simple example on how to do switch statements with int values:
/**
 * A simple example on how to use switch statement in Groovy.
 */
class SwitchExample {
   static main(args) {
      int num = 3;
      switch (num) {
         case 1: println "One"; break;
         case 2: println "Two"; break;
         case 3: println "Three"; break;
         default: println "Something else";
      }
   }
}

As expected, the result is below:

Three

Range of Values

In Groovy, we can have have switch statement where a case tries to match a range of values. Below is a simple example for that:

/**
 * A simple example on how to use Groovy switch statement with range of values.
 */
class SwitchExample {
   static main(args) {
      int num = 3;
      switch (num) {
         case 1..2: println "One or Two"; break;
         case 3..4: println "Three or Four"; break;
         case 5..6: println "Five or Six"; break;
         default: println "Something else";
      }
   }
}

The sample shows tests for range of values for each case. The result of the code when executed is below:

Three or Four

Regular Expression

We can also use Groovy switch statements with regular expressions. Below is a simple example where each case uses matching with regular expressions.

/**
 * A simple example on how to use Groovy switch statement with regular expressions.
 */
class SwitchExample {
   static main(args) {
      String str = "12345"
      switch (str) {
         case ~/^[A-Za-z]+$/: println "Alphabet only"; break;
         case ~/^[0-9]+$/: println "Numeric only"; break;
         default: println "Others";
      }
   }
}

The expected output is:

Numeric only

Matching of Class

We can also try to match the class of the value in the switch statement. Below is an example where each case tries to check the class of the value:
/**
 * A simple example on how to use Groovy switch statement with matching of class.
 */
class SwitchExample {
   static main(args) {
      String str = "Test"
      switch (str) {
         case String: println "${str} is a String"; break;
         case Integer: println "${str} is an Integer"; break;
         default: println "Unknown";
      }
      Integer num = 100
      switch (num) {
         case String: println "${num} is a String"; break;
         case Integer: println "${num} is an Integer"; break;
         default: println "Unknown";
      }
   }
}

Since the first value is a String and the second value is an Integer, the output is below:

Test is a String
100 is an Integer

More Complex Matching

We can have more complex tests inside each case. Below is an example that test if a number is zero, negative, or positive.

/**
 * A simple example that uses Groovy switch statement to check if a number is zero, negative, or positive.
 */
class SwitchExample {
   static main(args) {
      int num = -5;
      switch (num) {
         case 0: println "Zero"; break;
         case { it < 0}: println "Negative"; break;
         default: println "Positive";
      }
   }
}

This is the expected result.

Negative

It is also possible if you need a long expression for each case. Below is an example:

/**
 * A simple example that uses Groovy switch statement to test divisors.
 */
class SwitchExample {
   static main(args) {
      Integer num1 = 100
      switch (num1) {
         case { it % 2 == 0 && it %5 == 0}: println "${num1} is divisible by 2 and 5"; break;
         case { it % 3 == 0}: println "${num1} is divisible by 3"; break;
         default: println "Others";
      }
      Integer num2 = 9
      switch (num2) {
         case { it % 2 == 0 && it %5 == 0}: println "${num2} is divisible by 2 and 5"; break;
         case { it % 3 == 0}: println "${num2} is divisible by 3"; break;
         default: println "Others";
      }
   }
}

As shown, you can do complex testing on each case, to suit your requirements. Below is the expected output:

100 is divisible by 2 and 5
9 is divisible by 3