Switch statement is a decision-driven statement that is used to test a variable/expression against a list of values. These values are referred as cases and the variable will be tested for each case. One switch expression/statement can have multiple cases therefore switch statement can be used to select a specific code block from multiple choices.

This article demonstrates the comprehensive guide of the following concepts regarding Java’s switch statement:

So, Let’s get started!

What is switch statement

It is a mechanism that is used to control the flow of a program by allowing a variable or an expression to be tested against a list of values. A complete guide for the switch statements is provided in the syntax section.

Basic Syntax

The basic syntax of Java switch statement will be:

switch(expression) {


   case A:


      // statement(s)


      break; //optional


   case B:


      // statement(s)


      break; //optional


   default: //optional


      //statement(s)

}

  • Switch statement takes an expression and compares its value with all the cases.
  • Each case must have a unique name.
  • If the perfect match for the case is found then the block of statements associated with that case will be executed.
  • default is an optional statement, the code/statements associated with the default case will execute when no match found.
  • break is also an optional statement that is used to terminate the case

Let’s understand the break and default statements in a little more detail to have a clear understanding.

Break in Switch Statement

Break keyword is used as a control statement to terminate a case in switch statement. Use of break statement is optional and it must be written in small letters. The syntax of the break statement is mentioned below:

Default in Switch Statement

Multiple cases can be specified in switch statement, however, it is possible that the condition/value you are looking for doesn’t find a perfect match in all the switch cases. Therefore, we can specify a default case that will be executed if no match found. The syntax of the default statement is provided below:

How Switch statement Works

Let’s consider the below-given example to understand how switch statement work.

Example

This example takes marks from the student and finds the grade:

public static void main(String[] args) {

int marks;


Scanner scan = new Scanner(System.in);

System.out.println(“Enter Your Marks between 0 to 100”);


marks = scan.nextInt();

   if(marks<0 || marks>100)

   {

      System.out.println(“Enter a Valid Number”);

   }

   else{

   switch( marks / 10 )


   {


      case 10:


      {


         System.out.println(“Grade: A “);


         break;


      }


      case 9:


      {


         System.out.println(“Grade: A “);


         break;


      }


      case 8:


      {


         System.out.println(“Grade: B “);


         break;


      }


      case 7:


      {


         System.out.println(“Grade: C “);


         break;


      }


      case 6:


      {


         System.out.println(“Grade: C”);


         break;


      }


      case 5:


      {


         System.out.println(“Grade: D”);


         break;


      }


      default:


      System.out.println(“Grade: F”);

}

}

}

The condition of the switch case is (marks/10), now let’s say the user entered “78”.

The entered number will be divided by 10 i.e. 78/10

We will get the result equal to 7 because in java “/” operator divides the two numbers and consider the quotient as a result.

Therefore the switch statement goes for case “7” and the code associated with the case “7” will be executed:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image1-53.png" data-lazy- height="142" src="data:image/svg xml,” width=”693″>

Let’s assume that the user entered a number greater than 100, then we will get the following output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image2-55.png" data-lazy- height="131" src="data:image/svg xml,” width=”470″>

Now, the user entered 130 which is greater than 100 so, switch statement wouldn’t execute.

Conclusion

The switch statement in java is used to perform different tasks on the basis of condition provided. The break and default statements/keywords are optional in switch statement, the break statement can be used to come out of the switch body, and the default keyword can be used to specify a generic case that will execute if no match found in a switch case.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/FA9AFCF66E2D44C3A65378AC87A87EB4-150×150.jpg621edbc0d953c.jpg" height="112" src="data:image/svg xml,” width=”112″>

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.