United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 4269827 Allow multiple constants and ranges in case labels
4269827 : Allow multiple constants and ranges in case labels

Details
Type:
Enhancement
Submit Date:
1999-09-08
Status:
Open
Updated Date:
2009-02-23
Project Name:
JDK
Resolved Date:
Component:
specification
OS:
generic,windows_xp
Sub-Component:
language
CPU:
x86,generic
Priority:
P4
Resolution:
Unresolved
Affected Versions:
1.2.2,6u29
Targeted Versions:

Related Reports
Duplicate:
Relates:
Relates:

Sub Tasks

Description

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) 
======================================================================

                                    

Comments
WORK AROUND



Name: wl91122			Date: 09/08/99


Use separate case labels for each case, which is less readable.

Use if statements for ranges. Again, less readable.
======================================================================
                                     
2004-07-13
EVALUATION

Relatively harmless syntactic sugar.

gilad.bracha@eng 1999-09-08
                                     
1999-09-08



Hardware and Software, Engineered to Work Together