Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4269827
Votes 18
Synopsis Allow multiple constants and ranges in case labels
Category java:specification
Reported Against 1.2.2
Release Fixed
State 6-Fix Understood, request for enhancement
Priority: 4-Low
Related Bugs 4276296 , 5012262
Submit Date 08-SEP-1999
Description




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




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

Use if statements for ranges. Again, less readable.
======================================================================
Evaluation
Relatively harmless syntactic sugar.

  xxxxx@xxxxx   1999-09-08
Comments
  
  Include a link with my name & email   

Submitted On 16-JAN-2001
DaleKing
It is actually a little more than syntactic sugar. It can 
produce more optimal code than using if's. Consider this 
switch statment using ranges:

switch( c)
{
    case 'a', 'e', 'i', 'o', 'u':
        System.out.println( &quot;vowel&quot; );
        break;
    case 'b'..'d', 'f'..'h', 'j'..'n', 'p'..'t', 'v'..'z':
        System.out.println( &quot;consonant&quot; );
        break;
    case '0'..'9':
        System.out.println( &quot;digit&quot; );
        break;
}

Consider how you would do that in a readable fashion with 
if's. What is the maximum number of comparisons. A naive 
reproduction of the switch could take up to 17 comparisons. 
Here is a simpler version that could take up to 9:

if( c &gt;= 'a' &amp;&amp; c &lt;= 'z' )
{
    if( c == 'a' || c == 'e' || c == 'i' || c == 'i' || c 
== 'i' )
    {
        System.out.println( &quot;vowel&quot; );
    }
    else
    {
        System.out.println( &quot;consonant&quot; );
    }
}
else if( c &gt;= '0' || c &lt;= '9' )
{
    System.out.println( &quot;digit&quot; );
}

But that is still not optimal. Here is the pseudocode for 
the trivial optimization that takes 4 comparisons worst 
case:

if( c &gt;= 'i' )
    if( c &gt;= 'p' )
        if( c &lt;= 'z' )
            if( c == 'o' )
                goto vowel;
            else
                goto consonant;
    else
        if( c &lt; 'j' )
            goto vowel;
        else
            if( c == 'o' )
                goto vowel;
            else
                goto consonant;
else
    if( c &lt; 'a' )
        if( c &lt;= '9' )
            if( c &gt;= '0' )
                goto digit;
    else
        if( c == 'a' )
            goto vowel;
        else
            if( c == 'e' )
                goto vowel;
            else
                goto consonant;

Such an optimization is simple with a switch statement, but 
would be very difficult with a series of if statements. 
With if statements the compiler would have to guarantee 
that the value does not change.


Submitted On 07-MAR-2001
lotusalife
Both ranges and comma separated lists seem quite desirable,
from the point of view of the programmer.


Submitted On 25-JUL-2001
DaleKing
In addition it would be desirable to have some cases like:

// All values less than 56 without requiring that the
// minimum be explicitly specified.
case <56:

// How about a range check using:
case <0, >=100 : // throw Exception

One could also allow floating point values for switch 
statements in which case you need to specify ranges that 
include or exclude the endpoint:

// The range [0,100)
case 0.0 .. <100.0 :

// The range (0,100], the <= is redundant
case >0.0 .. <=100.0 :

Or you could figure out a way to actually specify ranges 
using the parenthesis and bracket notation, but that would 
probably be a major tear-up to parsing. 


Submitted On 16-AUG-2002
rlorenzo
Don't ignore this improvement!... I want to see this done.


Submitted On 04-FEB-2003
manihss
I fully agree that the fall through should not have been the 
default behaviour. Would be nice if it was the 'opposite' and a 
fall through with an explicit 'continue'..


Submitted On 26-FEB-2004
jdcjdc
haha. This shows the superiority of Pascal/Ada (which have
this feature) over languages the hack C from which Java is
partially derived.



PLEASE NOTE: JDK6 is formerly known as Project Mustang