July 30, 2020

Switch Statement in Java with Example

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.

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
}

Significant Standards for Switch Statement

These are some important rules of the 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.

To get in-Depth knowledge on Java you can enroll for a live demo on core Java Online Training

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");  
      }  
   }
}

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

Take your career to new heights of success with Java Training

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

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.

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-

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-

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.

Get More Info Java Certification Course