a groovy switch case statement with several variables
Asked Answered
F

3

5

Is it possible to have a switch-case statement with more than a variable in groovy? I tried with tuples but the case part doesn't accept more than one argument.

I am trying to avoid several nested if statements so instead of

if (a==1) {
  if (b==2) {
    if (c==3) {
      // do something
    }
  }
}
else {
  if (a==4) {
    if (b==5) {
      if (c==6) {
        //do something else 
      }
    }
  }
}

Can I do:

switch(a,b,c) { 
  case : (1,2,3) // if a==1, b==2 and c==3
    // do something 
    ... 
  case : (4,5,6)
    // do something else  
    ... 
  } 
}
Farver answered 16/1, 2019 at 16:30 Comment(0)
F
7

Groovy is just dirty java, you don't need any class definition. everything you write in a java method you can write it directly in groovy.

switch (num) {
case 1:
case 2:
case 3:
   System.out.println("1 through 3");
   break;
case 6:
case 7:
case 8:
    System.out.println("6 through 8");
 break;
}

To answer your question, inside the switch we need an expression, not function parameters.

Flemming answered 16/1, 2019 at 16:57 Comment(5)
thanks. That would work for one criteria and several values, but I need to test on a set of criteria. I edited my question for (hopefully) more clarityFarver
if it has multiple criteria, then use if() like.. if (a==1 && b==2 && c==3){do this ;} else if( a==4 && b==5 && c==6){then do that;} else if() ........Flemming
You mean "groovy is just way nicer java", right? :-)Gushy
Yeah, we don't need any class or method definition. Just write it as a script and tell it what to do.. and it will do. :) But if one needs to create methods and classes , it can be done. I used it to create scriptsFlemming
Groovy is just dirty java A bit irrelevant but this made my day!! Thanks @FlemmingCounterstroke
G
3

Based on your edit, I believe that this should work:

if (a == 1 && b == 2 && c == 3) {
  // do something
} else if (a == 4 && b == 5 && c == 6) {
  // do something else
}

If you want a switch statement instead, that's possible:

def val = [a, b, c]
switch (val) {
    case {it == [1, 2, 3]}:
        // something
        break;
    case {it == [4, 5, 6]}:
        // something else
        break;
Gushy answered 16/1, 2019 at 17:36 Comment(4)
Obviously any switch can be defined in an if-else chain, but when you have >2 cases switch just looks betterAbbotsen
Looking at this again now (I had forgotten about it) I completely agree with you...switch is much more readable.Gushy
@Gushy can you elaborate on what this groovy/java construct is that putting a condition in curly braces makes it work for a case condition? Just a term I can search for would be enough. Thanks.Kiethkiev
I'd search for "groovy closure".Gushy
C
0
class Solution{
 static void main (String...args){
BufferedReader br=new BufferedReader(new 
InputStreamReader(System.in))
def val=br.readLine()        
     switch(val){
       case('E0'):
         println "Basic"
         break;
         default:
         break;
         case('E1'):
         println "Inter"
         break;
         case('E2'):
         println "Advance"
         break;
       default:
       println "not defined"
       
              }
          
  }
}
Cigar answered 28/2, 2022 at 6:3 Comment(2)
unable to achieve the all ouputCigar
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Forthcoming

© 2022 - 2024 — McMap. All rights reserved.