Java
TOPIC 16 – IF STATEMENTS – PART 3

AP EXTRA

 

 

AP LESSON NOTE

 

 

SWITCH STATEMENT

 

The switch structure is a rarely used structure that can replace the if / else if / else structure when possible test values are exact numbers (and not ranges).  Because of its limitation of needing exact numbers and the fact that it’s error prone, it is a structure that is not often used by computer scientists.

 

Nevertheless, it is always possible that a programmer would encounter such a structure in somebody else’s code (or on an AP exam).  Therefore, it is important to understand its basic functionality.

 

GENERAL SWITCH TEMPLATE

 

In the template below, we test the variableToTest against value1, value2, value3, … until we have a match or run out of values.  If we have a match, we execute the associated statement block and break to the bottom.  If we have no matches, we run the statement block inside the default case.

 

switch (variableToTest)

{

   case value1:

        statement block 1;

        break;

   case value2:

        statement block 2;

        break;

   case value3:

        statement block 3;

        break;

   default:

        statement block;

        break;

     }

 

EXAMPLE

 

Use a switch structure to get name of the month associated with the number.  So for, 1, you should get the name “January”.

 

System.out.println("Enter a month's number(1 to 12)");

int month = DummiesIO.getInt();

String monthString;

 

switch (month)

{

   case 1:  monthString = "January";

            break;

   case 2:  monthString = "February";

            break;

   case 3:  monthString = "March";

            break;

   case 4:  monthString = "April";

            break;

   case 5:  monthString = "May";

            break;

   case 6:  monthString = "June";

            break;

   case 7:  monthString = "July";

            break;

   case 8:  monthString = "August";

            break;

   case 9:  monthString = "September";

            break;

   case 10: monthString = "October";

            break;

   case 11: monthString = "November";

            break;

   case 12: monthString = "December";

            break;

   default: monthString = "Invalid month";

            break;

}

System.out.println(monthString);

 

EXAMPLE 2

 

This example is the opposite from the previous.  Given a month’s name, the code figures out the month’s number.  So, “january” gives 1.

 

System.out.println("Enter your favourite month (all lower case letters)");

String month = DummiesIO.getString();

 

int monthNumber;

 

switch (month)

{

   case "january":

            monthNumber = 1;

            break;

   case "february":

            monthNumber = 2;

            break;

   case "march":

            monthNumber = 3;

            break;

   case "april":

            monthNumber = 4;

            break;

   case "may":

            monthNumber = 5;

            break;

   case "june":

            monthNumber = 6;

            break;

   case "july":

            monthNumber = 7;

            break;

   case "august":

            monthNumber = 8;

            break;

   case "september":

            monthNumber = 9;

            break;

   case "october":

            monthNumber = 10;

            break;

   case "november":

            monthNumber = 11;

            break;

   case "december":

            monthNumber = 12;

            break;

   default:

            monthNumber = 0;

            break;

}

 

if (monthNumber == 0)

{

   System.out.println("Invalid month.");

}

else

{

   System.out.println("Month number: " + monthNumber);

      }

 

RANDOMNESS WITH SWITCH

 

We can create randomness by generating a random value and using a switch statement.  Of course, we’ve already done this using an if-statement.

 

EXAMPLE – Randomly generate a different comment based on the value of a random number.

 

      String comment;  

      int which = (int)(Math.random() * 3);    //Random result is 0, 1, or 2.

 

      switch (which)

      {

          case 0:  comment = "You look so much better than usual.";

                   break;

          case 1:  comment = "Your work is up to its usual standards.";

                   break;

          case 2:  comment = "You're quite competent for so little experience.";

                   break;

          default: comment = "Oops -- something is wrong with this code.";

      }