0

I want to compare array length with some int values. I can do it with if else but how to do it with switch because switch is fast and I want to use that in my project

switch (array.length) {
    case array.length <= 11: // how to do this 
        break;
    default:
        break;
}

With if else I can do this:

if (array.length <= 5) { 
    //my is code here 
}
else if (array.length <= 15) {
    //my is code here 
}
else if (array.length <= 10) {
    //my is code here 
}
user253751
  • 57,427
  • 7
  • 48
  • 90
AHSAN KHAN
  • 426
  • 7
  • 17
  • I this [this](http://stackoverflow.com/questions/10873590/in-java-using-switch-statement-with-a-range-of-value-in-each-case/19062152#19062152) answer is clever – Lrrr Jun 06 '15 at 12:35

5 Answers5

4

You can't. switch can only test exact values.

You could do:

switch(array.length) {
case 0: case 1: case 2: case 3:
case 4: case 5: case 6: case 7:
case 8: case 9: case 10: case 11:
    // do stuff
    break;
default:
    break;
}

But why do you want to do this? What makes you think it's faster?

user253751
  • 57,427
  • 7
  • 48
  • 90
0

switch is not the same as if (...) { ... } else { ... }. You can only use == within a case. You would have to do something like this:

int length = array.length;
switch (length) {
    case 0:
    case 1:
    case 2:
    [...]
    case 11:
        // your code here
        break;
    //other cases here
}

Notice the missing break-statements, they are quite important. I would recommend this tutorial for more details.

Turing85
  • 18,217
  • 7
  • 33
  • 58
0

You can't do it(as per your example) using switch. Since the values of case are constant expression (case *value*).

Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
0

Switch statements operate by exact matches rather than comparisons like if does. You can do what you want by introducing a new variable like this:

int value = (array.length <= 11 ? 0 : (array.length <= 20 ? 1 : 2));
switch (value) {
    case 0:  // 11 or under
    break;
    case 1:  // 12 to 20
    break;
    case 2:  // 21 or more
    break;
}

I don't know if this buys you much over if/else statements, but if you find it cleaner to code, you can do it.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
0

In your if/elseif, the if(array.length<=10) will never run because if(array.length<=15) is checked above it.

Doing this with an if/elseif structure would require less lines of code. If you want to do this using a switch/case statement, this would work:

int length = array.length;

    switch(length) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5: {
        System.out.println("Case 0<=length<=5 triggered.");
        break;
    }
    case 6:
    case 7:
    case 8:
    case 9:
    case 10: {
        System.out.println("Case 6<=length<=10 triggered.");
        break;
    }
    case 11:
    case 12:
    case 13:
    case 14:
    case 15: {
        System.out.println("Case 10<=length<=15 triggered.");
        break;
    }
    default: {
        System.out.println("Default case triggered.");
    }
    }
Pieter12345
  • 1,713
  • 1
  • 11
  • 18