Switch statement in java

Let’s learn switch statement in java.

Switch statement in java

Java switch statement executes statements based on value of variable or expression. Switch statement evaluate expression first and then execute all statements following matching case label.

Let’s see syntax and java program to demonstrate switch case with primitive integer data type.

Syntax

switch(expression)
{
   case 1:
   // code goes here
   break;

   case 2:
   // code goes here
   break;
   
   case 3:
   // code goes here
   break;
   .
   .
   .
   .
   case N:
   // code goes here
   break;

   default:
   // default code goes here
}



switch statement in java
Flow diagram of switch case
public class SwitchStatementJava
{
   public static void main(String[] args) 
   { 
      int year = 2;
      switch(year) // switch statement is evaluated only once
      {
         case 1:
             System.out.println("January");
             break; // break keyword is optional

         case 2:
             System.out.println("February");
             break;

         case 3:
             System.out.println("March");
             break;

         case 4:
             System.out.println("April");
             break;

         case 5:
             System.out.println("May");
             break;

         case 6:
             System.out.println("June");
             break;

         case 7:
             System.out.println("July");
             break;

         case 8:
             System.out.println("August");
             break;

         case 9:
             System.out.println("September");
             break;

         case 10:
             System.out.println("October");
             break;

         case 11:
             System.out.println("November");
             break;

         case 12:
             System.out.println("December");
             break;

         default: // default keyword is optional
             System.out.println("Default case");
      } 
   }
}



Output :

February

break keyword

Switch statement is used with “break” keyword. When code reaches break keyword, it comes out of switch block.

Also read – java program to swap two numbers

By using break keyword we can stop execution. Since there is no need for more case testing. Hence saving execution time.

default keyword

default keyword is used if there is no case match. default keyword defines some default code. Refer above java program.


NOTE :

  1. In the above example we can’t use break keyword after default, because control itself come out of switch after default.
  2. Meanwhile there’s no harm using break keyword after default.
  3. Duplicate case value are not allowed. Case value should be of same data type.
  4. Expression inside switch should result in constant value else would be invalid.
  5. Case need not have to be in order you can specify them in any order. Also, it need not be in ascending order.
  6. In switch statement variable used are byte, short, integer, strings and enums.
  7. We can have any number of case statements within switch.
  8. Data type for a case must be same.
  9. We can also use characters in switch statement.
  10. Valid expressions for switch statement are as below,

Valid: switch(5 + 6 + 56) switch(5 * 6 + 7 % 8)
Invalid: switch(pq + rs) switch(p + q + r)


enum in switch case

Switch case statement can be used with switch case enum. Enum types are special data type that enables for variable to be a set of predefined constants.

Enumerated data type are constants hence enum fields are in uppercase. Let’s see program to demonstrate how to use Enum in Switch case statement.

public class SwitchEnumDemo 
{
   public enum Day 
   {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 
   }

   Day day;
   public SwitchEnumDemo(Day day) 
   {
      this.day = day;
   }

   public void weekMethod() 
   {
      switch (day) 
      {
         case MONDAY:
             System.out.println("week day monday");
             break;

         case TUESDAY:
             System.out.println("week day tuesday");
             break;

         case WEDNESDAY:
             System.out.println("week day wednesday");
             break;

         case THURSDAY:
             System.out.println("week day thursday");
             break;

         case FRIDAY:
             System.out.println("week day friday");
             break;

         case SATURDAY:
             System.out.println("week end saturday");
             break;

         case SUNDAY:
             System.out.println("week end sunday");
             break;

         default:
             System.out.println("not a week day or week end");
             break;
      }
   }

   public static void main(String[] args) 
   {
      SwitchEnumDemo day1 = new SwitchEnumDemo(Day.MONDAY);
      day1.weekMethod();
      SwitchEnumDemo day2 = new SwitchEnumDemo(Day.TUESDAY);
      day2.weekMethod();
      SwitchEnumDemo day3 = new SwitchEnumDemo(Day.WEDNESDAY);
      day3.weekMethod();
      SwitchEnumDemo day4 = new SwitchEnumDemo(Day.THURSDAY);
      day4.weekMethod();
      SwitchEnumDemo day5 = new SwitchEnumDemo(Day.FRIDAY);
      day5.weekMethod();
      SwitchEnumDemo day6 = new SwitchEnumDemo(Day.SATURDAY);
      day6.weekMethod();
      SwitchEnumDemo day7 = new SwitchEnumDemo(Day.SUNDAY);
      day7.weekMethod();
   }
}



Output:

week day monday
week day tuesday
week day wednesday
week day thursday
week day friday
week end saturday
week end sunday


java switch string

Now let’s learn string in switch case in java. String in switch case is better than if-else. Here’s java program to demonstrate use of string to control switch statement,

public class SwitchStringJava 
{
   public static void main(String[] args) 
   {
      String str = "twelve"; 
      switch(str) 
      { 
         case "ten": 
             System.out.println("ten"); 
             break;
         case "eleven": 
             System.out.println("eleven"); 
             break;
         case "twelve": 
             System.out.println("twelve"); 
             break; 
         default: 
             System.out.println("doesn't match"); 
      }
   }
}



Output:

twelve

Grade program

Now let’s see grade program using switch statement in java. Here we have to compute marks based on given grades.

So if grade is A then marks will be greater than or equal to 80, if grade is B then marks will be greater than or equal to 60 and less than 80.

If grade is C then marks will be greater than or equal to 40 and less than 60. Lastly if grade is F then marks will be less than or equal to 40.

Print invalid grade if any other grade is typed by user. Here’s the grade program using switch,

import java.util.Scanner;

public class GradeSwitch 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter grade from A, B, C or F: ");
      String strInput = sc.next();
      char grade = strInput.charAt(0);
      switch (grade) 
      {
         case 'A':
             System.out.println("Grade A - distinction");
             break;
         case 'B':
             System.out.println("Grade B - first class");
             break;
         case 'C':
             System.out.println("Grade C - second class");
             break;     
         case 'F':
             System.out.println("Grade F - fail");
             break;
         default:
             System.out.println("invalid grade");
             break;
      }
      sc.close();
   }
}



Output:

Please enter grade from A, B, C or F): A
Grade A – distinction

Please enter grade from A, B, C or F): E
invalid grade