Java - Switch Case Statements


Java - Switch Case Statements

  • Permits a variable to be tested for sameness against a set of values. 
  • Allows multi-way branching.
  • Each value is termed as case.
  • Variable being switched on is tested in each case
  • Switch expression type can be a char, byte, short, or int.
  • Case values should be literals and compatible with the expression.
  • No two case values must be same.  
  • The default statement is optional.
  • It is executed in case no match found for the expression.
  • The break statement is optional.
  • Can be nested.


Java Switch Case

Syntax :

  switch(expression)
  {
    case value :
       //Set of Statements
       break; //optional

    case value :
       // Set of Statements
       break; //optional

    //Multiple case statements are allowed
    default: //Optional
    	   // Set of Statements
   break;
  }

Sample Code :

class MySwitchSample 
{
    public static void main(String[] args) 
    {
        System.out.println("\n\nWikiTechy - Switch Case Statements\n"); 
        int week = Integer.parseInt(args[0]);
        switch (week) 
        {
            case 1:  System.out.println("First day of the week: Sunday");
            break;
            case 2:  System.out.println("Second day of the week: Monday");       
            break;
            case 3:  System.out.println("Third day of the week: Tuesday"); 
            break;
            case 4:  System.out.println("Fourth day of the week: Wednesday"); 
            break;
            case 5:  System.out.println("Fifth day of the week: Thursday");  
            break;
            case 6:  System.out.println("Sixth day of the week: Friday");
            break;
            case 7:  System.out.println("Seventh day of the week: Saturday"); 
            break;
            default: System.out.println("Invalid data for Week");
            break;
        }
    } 
}

Code Explanation :


    Reading command line argument value and converting it to integer using the code  int week = Integer.parseInt(args[0]);

    Switch statement with expression as the variable week is indicated by code switch (week).

    Different case statements are defined to branch upon based on the value of variable week as follows :

case 1:  System.out.println("First day of the week: Sunday");
break;
case 2:  System.out.println("Second day of the week: Monday");       
break;
case 3:  System.out.println("Third day of the week: Tuesday"); 
break;
case 4:  System.out.println("Fourth day of the week: Wednesday"); 
break;
case 5:  System.out.println("Fifth day of the week: Thursday");  
break;
case 6:  System.out.println("Sixth day of the week: Friday");
break;
case 7:  System.out.println("Seventh day of the week: Saturday"); 
break;
           

    The default statement is defined and executed when none of case values matches with the value of variable week. This is done by the code 

 default: System.out.println("Invalid data for Week");
 break;

Output :


    Command line argument input is given as 6 while executing the code in the output console. This is performed by the line

java MySwitchSample 6

    The value 6, matches with case 6 as per the code and hence the output in the console window is: Sixth day of the week: Friday