switch statement in java

Let’s learn switch statement in java.

switch statement in java

switch statement is used to choose one of the few blocks of code to get some output.

Here in switch statement switch expression is evaluated once and compared with each “case” value.

Then if there is a match with any of the “case”, corresponding code block is executed.

switch statement in java
Flow diagram of switch case

The value of case should be a literal or constant. If the variable being switched is equal to case then statements corresponding to that case will execute until break statement.

switch terminates when it is reached break statement. Then the control shifts to next switch statement.

It’s not mandatory to add break statement in every case.

If there’s no break statement then flow of control will continue to next subsequent case until break statement.

It is a good programming practice to have a default case in switch statement which should appear at the end.

default case can be used to perform a task when none of the cases is true.

Datatypes used in switch statement should be enums, strings, convertable integers (char, byte, short) and integers.

We can have any number of case statements in switch expression. Value for “case” must be same datatype as variable in switch.

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

Let’s see syntax of switch statement.

Syntax

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

   case 2 value :
   // code goes here
   break;

   case 3 value :
   // code goes here
   break;
   .
   .
   .
   .
   
   default: // optional
   // default code goes here
}


switch case in java example programs

public class SwitchStatementExample
{
   public static void main(String[] args)
   {
      char grade = 'A';
      switch(grade) {
          case 'A' :
              System.out.println("Distinction.");
              break;
          case 'B' :
          case 'C' :
              System.out.println("First class.");
              break;
          case 'D' :
              System.out.println("You have passed.");
          case 'F' :
              System.out.println("Fail. Try again.");
              break;
          default :
              System.out.println("Invalid grade");
      }
      System.out.println("Grade is: " + grade);
   }
}


Output:

Distinction.
Grade is: A


grade program using switch case

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 statement.

import java.util.Scanner;
public class GradeSwitch 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("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


Also read – TreeMap in java