0

switch (x)
{
case A:
case B:
case C:
.doSomething()
}
Is there a way I could have those 3 cases in one line? For example something like this
case A, B, C:?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Oscar F
  • 323
  • 2
  • 9
  • 21

3 Answers3

9

Aside from removing the line breaks and putting those cases all on the same line, no.

You will have to have three case keywords and three :'s.

If you want details, see section 14.11 in the JLS. In particular:

SwitchLabel:
    case ConstantExpression :
    case EnumConstantName :
    default :

There is no pattern in the grammar that would accept something like case A,B,C: for a SwitchLabel.

It is a common practice, though, to structure cases as in your example when multiple cases do the same thing:

switch (value) {
case 1:
case 3:
case 5:
    System.out.println("It's a positive odd number less than 7!"); 
    break;
case 4:
case 8:
    System.out.println("It's a multiple of 4 between 1 and 9!");
    break;
default:
    System.out.println("It's just another boring number.");
    break;
}

Java programmers will generally get a clear understanding of code like that when they read it. Putting multiple cases on a single line (i.e. no line break) is much less common and will not be as clearly understood at a glance by a typical programmer (who would probably just think you accidentally deleted a line break).

Jason C
  • 38,729
  • 14
  • 126
  • 182
1

Not the way you suggest, but you can do this (from the documentation):

class SwitchDemo2 {
    public static void main(String[] args) {

        int month = 2;
        int year = 2000;
        int numDays = 0;

        switch (month) {
            case 1: case 3: case 5:
            case 7: case 8: case 10:
            case 12:
                numDays = 31;
                break;
            case 4: case 6:
            case 9: case 11:
                numDays = 30;
                break;
            case 2:
                if (((year % 4 == 0) && 
                     !(year % 100 == 0))
                     || (year % 400 == 0))
                    numDays = 29;
                else
                    numDays = 28;
                break;
            default:
                System.out.println("Invalid month.");
                break;
        }
        System.out.println("Number of Days = "
                           + numDays);
    }
}
Vidya
  • 29,932
  • 7
  • 42
  • 70
0

For example when declaring integer values, you could have int x int y int zetc. or you could have int x, y, z I'm just wondering if that's possible with cases.

That is a complete different question and it is possible depending on what you are doing.

You can have nested switches like

switch(x) {
      case 1:
          switch(y) {
               case 2:
                    switch(z) {
                        case 3:

Or you can use a formula provided you know the range of possible values like this

switch(x * 100 + y * 10 + z) {
    case 123: // x = 1, y = 2, z = 3

obviously this assumes x, y, z are between [0..9]

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130