toplogoconditionals
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

sign

The "switch" Statement

If your program must select from one of many different actions, the "switch" structure will be more efficient than an "if ..else..." structure.

In the "switch" statement, a variable is tested for equality against a list of values, referred to as cases.

switch (expression)
{
     case (expression 1):    {
                                            one or more Java statements;
                                            break;
                                         }
    case (expression 2):     {
                                           one or more Java statements;
                                           break;
                                        }
   case (expression 3):     {
                                          one or more Java statements;
                                          break;
                                        } 
                    . 
                    .
                    .

    default:                        {
                                        one or more Java statements;
                                        break;
                                        }
}


•  You MUST use a "break" statement after each "case" block to keep execution from "falling through" to the remaining case statements. The "break" statement terminates the switch.

•  Originally, only int, short, char or byte types could be used as control expressions in "switch" statements. But starting with Java 7, switch statements can also use string expressions. The switch statement will use the equals method of the String object to compare the strings. Watch out for case sensitivity.

•  It is best to place the most often used choices first to facilitate faster execution.

•  While the "default" is not required, it is recommended. It is a safety net to prevent an error should the expression NOT match any of the cases.

• Notice the use of the colon after each case.

dividerdash

If you want to have several choices give the same responses, you can use the following coding style:

switch (value)
{
     case (1):
     case (2):
     case (3):   {
                       //The case code for 1, 2, 3
                       break;
                       }
     case (4):
     case (5):
     case (6):   {
                      //The case code for 4, 5, 6
                      break;
                      }
     default:    {
                     //The code for other values
                     break;
                      }
}

dividerdash

Example with Strings: (coursecode is a String variable)

switch (coursecode)
{
     case ("B240"):   {
                                 System.out.println("Intro to Biology");
                                 break;
                                }
     case ("M360"):    {
                                 System.out.println("Linear Algebra");
                                  break;
                                }
     default:    {
                     System.out.println("Not a course code.");
                     break;
                    }
}