Implement Switch Statement in Java with Example

When we learn Decision Making Statement in Java, we hear a new term Switch Statements. Have you ever wondered what they are? How do these statements help us in decision-making? This article is purely designed to help you learn and implement the switch case statement in Java with examples.

Switch Statement in Java

Java switch case articulation is a multi-way branch explanation.

Switch statement in Java is basically used when we have multiple options. In other words, it executes only one statement from multiple conditions. This statement is like an if-else-if ladder. The switch statement works with a byte, short, int, long, enum and String types.

Master the concept of Data types in Java 

Java Switch Case Syntax

Here, we will discuss the syntax of a switch statement in Java.

switch(expression)
{
   case value1 :
// Statement
      break;
// break is optional
     case value2 :
// Statements
      break,
     default :
// Statements
}

switch statement flowchart

Significant Standards for Switch Statement

These are some important rules of Java switch case.

  • The cases shouldn’t be duplicated.
  • The values for a case must be similar data type as the variable in the switch.
  • Values for a case must be constant or literal.
  • The break statement is used inside the case to end a statement.
  • The break statement is not mandatory, If it is not used then the condition will continue to the next one.
  • The default statement is also not mandatory, but if the expression is not matching the case then nothing will execute.

Do you know What are Literals in Java?

Java Switch Case Examples

These are some pitfalls or examples of a switch statement in java.

Implement Arithmetic Operations

package SwitchStatementDemo;

public class SwitchProgram1 {
  public static void main(String[] args) {  
     
 //Declaring a variable for switch expression  
      int choice=20,operand1=20,operand2=10,sum; 
      
//Switch expression  
      switch(choice){  
    
  //Case statements  
     
 case 10: 
      	sum=operand1+operand2;
      	System.out.println("Addition od two numbers is ="+sum);  
      break;  
      
case 20: 
      	sum=operand1-operand2;
      	System.out.println("Subtraction of two numbers is ="+sum);  
      break;  
      
case 30: 
      	sum=operand1*operand2;
          System.out.println("Multiplication of two numbers is ="+sum);  
      break;  
      
case 40: 
      	sum=operand1/operand2;
          System.out.println("Division of two numbers is ="+sum);  
      break;  
      
case 50: 
      	sum=operand1%operand2;
          System.out.println("Modulus of two numbers is ="+sum);  
      break;  
      //Default case statement  
      
default:System.out.println("Not in the following cases");  
      }  
   }
}

switch case

In the following program, the case expression was valid but if the expression is invalid then default case will be executed.

Suppose the value of the expression will be 2 then the output will be-

default case

Omitting the break statement in Java

Break statement is optional, but in the switch case, the program execution will be continued if we don’t use it. For instance, consider the below program, it shows how the program will work.

Before we discuss the example, you should know the Variables in Java.

package SwitchStatementDemo;
public class SwitchProgram1 {
  public static void main(String[] args) {
  
     int choice=10,operand1=20,operand2=10,sum; //Declaring a variable for switch expression 
     
//Switch expression 
      switch(choice){  
      
//Case statements  
     
 case 10: 
      	sum=operand1+operand2;
      	System.out.println("Addition of two numbers is ="+sum);    
      
case 20: 
      	sum=operand1-operand2;
      	System.out.println("Subtraction of two numbers is ="+sum);    
     
 case 30: 
      	sum=operand1*operand2;
          System.out.println("Multiplication of two numbers is ="+sum);  
      break;  
      
case 40: 
      	sum=operand1/operand2;
          System.out.println("Division of two numbers is ="+sum);    
     
 case 50: 
      	sum=operand1%operand2;
          System.out.println("Modulus of two numbers is ="+sum);   
      //Default case statement  
      
default:System.out.println("Not in the following cases");  
    }  
  }
}

Output-

Without Break Statement in Java

Nested Switch-Case Statement

package SwitchStatementDemo;
public class NestedSwitchStatement {
  public static void main(String args[])
  {
    String Branch="CSE";//Declaring string
    int year=2;//Declaring integer
    
//switch expression
    switch(year)
    {
    
//case statement
    case 1:
    System.out.println("elective courses : Advance english, Algebra");
    break;
    
    case 2:
      {
        switch(Branch)
        {
        
    case "CSE":
        
    case "CCE":
        System.out.println("elective courses : Machine Learning, Big Data");
        
    case "ECE":
        System.out.println("elective courses : Antenna Engineering");
        break;
        
    default:
        System.out.println("Elective courses : Optimization");
        }
      }
    }
  }
}

Output-

Nested Switch-Case Statement in Java

Summary

Now, you are well familiar with the switch statement in Java. Its time to implement and use it according to your requirements. This type of statement can help you with better decision making. Every beginner should master this concept to gain expertise in programming.

The next step is – Loops in Java with Examples

Hope, you liked the explanation. Share your feedback and suggestions in the comment section.

1 Response

  1. Riya says:

    Give some examples more to understand the theory

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.