News Ticker

Switch Statements and Gotchas

Switch

Format:

[sourcecode language=”java” toolbar=”false”]
switch (x){
case 1:
System. out .println(x);
break ;
case 2:
System. out .println(x);
break ;
default :
System. out .println(x);
break ;
}
[/sourcecode]

The switch argument:

  • Must be a value that can become an int implicitly:
    • byte, short, char, int;
    • Byte, Short, Character, Integer;
    • an enum.
  • Can be:
    • a variable reference,
    • a method call that returns a legal type,
    • an enum
  • Cannot be:
    • boolean, long, float or double
    • Boolean, Long, Float or Double

The case constant:

  • must be a compile time constant
    • a constant variable,
    • a final variable,
    • an enum.
  • Must evaluate to the same type as the switch argument type.

Example with enums:

[sourcecode language=”java” toolbar=”false”]
public enum Day { SUNDAY, MONDAY , TUESDAY , WEDNESDAY}

Day d = Day.MONDAY;

switch (d){
case MONDAY :
System. out .println(x);
break ;
case TUESDAY :
System. out .println(x);
break ;
default :
System. out .println(x);
break ;
}
[/sourcecode]

Example: where the switch argument is an int and the case argument are capable of implicitly becoming and int.

[sourcecode language=”java” toolbar=”false”]
final char a = ‘a’;
final char b = 1;
final char c = ‘\u9874’;

int x = 1;

switch (x){
case a: System. out.println( "1" ); break ;
case 1: System. out.println( "1" ); break ;
case ‘\u9874’ : System. out.println( "1" ); break ;
}
[/sourcecode]

Example: where the switch arguement is generated by a for loop:

[sourcecode language=”java” toolbar=”false”]
public static void main(String args[])
{
for ( int i = 0 ; i < 3 ; i++)
{
switch (i)
{
case 0: System. out.println(i); break;
case 1: System. out.println(i); break;
case 2: System. out.println(i); break;
}
}
}
[/sourcecode]

Example of the use of a post increment in the switch clause.

[sourcecode language=”java” toolbar=”false”]
public static void main(String args[])
{
int i;
LOOP: for (i=0; i<5;i ++)
{
switch (i++)
{
case ‘0’ : System. out.println( "A" );
case 1: System. out.println( "B"); break LOOP;
case 2: System. out.println( "C"); break ;
case 3: System. out.println( "D"); break ;
case 4: System. out.println( "E");
case ‘E’ : System. out.println( "F" );
}
}
}
[/sourcecode]

  1. the value of i used in the switch statement is 0,
  2. there is no case 0,
  3. the value of i is increased by 1 (i=1),
  4. and the next loop of the for is executed, the for increases the loop by 1 (i=2)
  5. the switch uses value of i = 2, and selects case 2.
  6. the value of i is increased by 1 (i=3) and the value 3 is printed,
  7. and the next loop of the for is executed, the for increases the loop by 1 (i=4)
  8. the switch uses value of i = 4, and selects case 4.
  9. the value of i is increased by 1 (i=5) and the value 5 is printed,
  10. the loop finishes

The value of i is used to select the case before it is increased.

Illegal:

  • Duplicate cases:
    • final char a = ‘a’;
    • and two cases
      • case a:
      • case 97:
    • These are duplicated cases, because their integer value is the same.
  • Cases larger than the switch argument:
    • A switch argument of type byte and a case argument of 128,
    • 128 is larger than the maximum number allowed in a byte.

The default:

  • only one allowed
  • can be placed anywhere
  • does not have an argument
  • acts just like a case statement

Example of valid switch statements:

[sourcecode language=”java” toolbar=”false”]

switch (1) { default : break; }

[/sourcecode]

An empty switch block is a valid construct

NOTE: A variable defined within a switch statement has scope through out the block:

[sourcecode language=”java” toolbar=”false”]
switch(0)
{
case 0 :
boolean b = false; //1
break;

case 1 :
b = true; //2
break;
}
[/sourcecode]

b is defined at line 1 and used after at line 2, this is not out of scope because it is within the {}. However b MUST be defined before it is used -> normal rules.

2 Trackbacks / Pingbacks

  1. Switch Statements | alex.theedom
  2. Switch Statements

Leave a Reply