/** * 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
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
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
/** * 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
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