The switch statement:   the multi-way selection statement

  • The switch statement:

    • is used to select one option among many to execute based on the value of an integer typed expression

  • Syntax:

     switch ( int-expr )
     {
        case intValue1: statement11; // Executed when int-expr == intValue1
                        statement12;
    		    ...
    		    break; // Cause the statements for case to end
        case intValue2: statement21; // Executed when int-expr == intValue2
                        statement22;
    		    ...
    		    break; // Cause the statements for case to end
        ...
        ...
       [default:        statementX1;   // Default case
                        statementX2;
    		    ...
    		    break; // Cause the statements for case to end  ]
     }

The switch statement example   testing if a number is odd or even

This Java program will read an integer and print out if it is odd or even:

import java.util.Scanner;

public class OddEven 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int x = input.nextInt();
       
       switch ( x % 2 )
       {
           case 0: System.out.println("Even");
	           break;
           case 1: System.out.println("Odd");  
	           break;
       } 
   }
}
  

DEMO: demo/03-selections/08-switch/OddEven.java

The switch statement example   print weekday, weekend or error

Program reads a number representing a day

If day == 1, 2, 3, 4, 5, then print weekday
If day == 6. 7, then print weekend
Otherwise: print error

   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);    
       int x = input.nextInt();
       
       switch ( x )
       {
           case 1: 
           case 2:
           case 3:
           case 4:
           case 5:  System.out.println("Weekday"); // Handle cases 1,2,3,4,5
                    break;
           case 6:
           case 7:  System.out.println("Weekend"); // Handle cases 6,7
                    break;
           default: System.out.println("Error");  
                    break;
       }   
   }

DEMO: demo/03-selections/08-switch/PrintDay.java

The switch statement is short-hand for a multi-way if-statement

A switch statement can always be replace by by a multi-way if-statement

Example:

import java.util.Scanner;

public class PrintDay2 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int x = input.nextInt();
       
       if ( x == 1 || x == 2 || x == 3 || x == 4 || x == 5 )
           System.out.println("Weekday");
       else if ( x == 6 || x == 7 )
           System.out.println("Weekend");
       else
           System.out.println("Error");
   }
}  

DEMO: demo/03-selections/08-switch/PrintDay2.java

The switch statement vs. the multi-way if-statement

The switch statement looks "nicer" than a multi-way if-statement

Here is a Java program that prints the Chinese Zodia sign for each year:

   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int year = input.nextInt();
       
       switch (year % 12) 
       {
         case 0:  System.out.println("monkey");    break;
	 case 1:  System.out.println("rooster");   break;
	 case 2:  System.out.println("dog");	   break;
	 case 3:  System.out.println("pig");	   break;
	 case 4:  System.out.println("rat");	   break;
	 case 5:  System.out.println("ox");	   break;
	 case 6:  System.out.println("tiger");	   break;
	 case 7:  System.out.println("rabbit");	   break;
	 case 8:  System.out.println("dragon");	   break;
	 case 9:  System.out.println("snake");	   break;
	 case 10: System.out.println("horse");	   break;
	 case 11: System.out.println("sheep");     break;
       }      
   }

DEMO: demo/03-selections/08-switch/ChineseZodiac.java