Hot Posts

Switch Statements and Gotchas

Switch

Format:

             switch (x){
                   case 1:
                        System. out .println(x);
                         break ;
                   case 2:
                        System. out .println(x);
                         break ;
                   default :
                        System. out .println(x);
                         break ;
            }
 

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:

       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 ;
      }
 

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

             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 ;
            }
  

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

  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;
         }
      }
   }
  

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

   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" );
            }
        }
    }
   
  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:


switch (1) { default : break; }

An empty switch block is a valid construct

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

      switch(0)
       {
           case 0 :
               boolean b = false; //1
               break;

           case 1 :
               b = true;  //2
               break;
       }
 

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.

1 Trackback / Pingback

  1. Switch Statements | alex.theedom

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: