Duplicate :
|
|
Relates :
|
|
Relates :
|
Name: wl91122 Date: 09/08/99 It would be nice if case labels allowed several comma separated values. switch( i ) { case 1,3,5,7: foo(); } would be equivalent to the less readable version switch( i ) { case 1: case 3: case 5: case 7: foo(); } In addition why not allow ranges as well? The two together allow you to efficiently code things like: switch( c ) { case 'A'-'Z','a'-'z': System.out.println( "letter" ); break; case '0'-'9': System.out.println( "number" ); break; default: System.out.println( "other" ); break; } As it stands now the only way to code this is using if statements but they aren't as readable as this switch statement. You can tell at a glance what this switch statement does. Compare that with: if( c >= 'A' && c <='Z' || c >= 'a' && c <='z' ) System.out.println( "letter" ); break; else if( c >= '0' && c <= '9' ) System.out.println( "number" ); break; else System.out.println( "other" ); break; I don't see any real problem with adding it to the language. Source code that uses this construct would not be backwards compatible, but neither is any code that uses a new API. The generated bytecode would still be backwards compatible. I really wish you could get rid of the fall-through behavior for switch statements, but I understand that it's too late now and it must be kept that way for backwards compatibility. (Review ID: 95013) ======================================================================
|