Switch Statement in Java | Java Switch Case With Examples

1. Objective

In our last tutorial, we discussed Java For Loop tutorial. Today, in this tutorial for Java, we are going to study the Switch statement in Java and various Java Switch Case examples. Moreover, We will also study Switch Case Syntax Java, a flow diagram of the switch statement in Java and many other things.

So, let us start with Switch Statement in Java.

Switch Statement in Java | Java Switch Case With Examples

Switch Statement in Java | Java Switch Case With Examples

2. Switch Statement in Java

Java switch case articulation is a multi-way branch explanation. It gives a simple method to dispatch execution to various parts of code in light of the estimation of the articulation. Essentially, the articulation can be a byte, short, singe, and int crude information composes. Starting with JDK7, it additionally works with specified kinds (Enums in Java), the String class and Wrapper classes in Java.

Do you Know How to Install Java in Windows & Linux

a. Switch Case Syntax Java

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

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

Flow Chart Switch Statement in Java

b. Important rules for a switch statement in Java

These are some important rules of Java switch case.

  • There shouldn’t be any duplicity between the cases.
  • The values for a case must be similar data type as the variable in the switch.
  • Values for a case must be steady or a literal. Variables are not permitted.
  • Break proclamation is utilized inside the change to end a statement succession.
  • Break proclamation is not mandatory. On the off chance that precluded, execution will proceed into the following case.
  • The default statement is also not mandatory, and it must show up toward the finish of the switch.

Let’s Explore Java Abstract Data Type in Data Structure 

3. Java Switch Case Examples

These are some pitfalls or examples of a switch statement in java.
a. This program displays the mane of the day with help of value of the day.

public class Test
{
    public static void main(String[] args)
    {
       int day = 5;
        String dayString;      
        switch (day)
        {
            case 1:  dayString = "Monday";
                     break;
            case 2:  dayString = "Tuesday";
                     break;
            case 3:  dayString = "Wednesday";
                     break;
            case 4:  dayString = "Thursday";
                   break;
            case 5:  dayString = "Friday";
                    break;
            case 6:  dayString = "Saturday";
                     break;
            case 7:  dayString = "Sunday";
                     break;
            default: dayString = "Invalid day";
                     break;
        }
         System.out.println(dayString);
    }
}

b. Omitting the break statement in Java
As break explanation is discretionary. In the event that we overlook the break, execution will proceed in the following case. It is now and then attractive to have different cases without break explanations between them. For instance, consider the updates adaptation of above program, it likewise shows whether a day is a weekday or an end of the weekday.

Read about Interface in Java with Example program

public class Test
{
    public static void main(String[] args)
    {   
        int day = 2;
        String dayType;
        String dayString;
        switch (day)
         {
            case 1:  dayString = "Monday";
                     break;
            case 2:  dayString = "Tuesday";
                     break;
            case 3:  dayString = "Wednesday";
                     break;
            case 4:  dayString = "Thursday";
                     break;
            case 5:  dayString = "Friday";
                     break;
            case 6:  dayString = "Saturday";
                     break;
            case 7:  dayString = "Sunday";
                     break;
            default: dayString = "Invalid day";
         }
           Switch (day)
           {
//multiple cases without break statements       
              case 1: 
              case 2:
              case 3:
              case 4:
              case 5:
              dayType = "Weekday";
                break;
              case 6:
              case 7:
              dayType = "Weekend";
                break;
              default: dayType= "Invalid daytype";
           }
         System.out.println(dayString+" is a "+ dayType);
     }
}

c. A nested Switch-Case statement in Java

public class Test
{
   public static void main(String[] args)
     {
        String Branch = "CSE";
        int year = 2;       
        switch(year)
           {
            case 1 :
                System.out.println("elective courses : Advance english, Algebra");
                break;
            case 2:
                switch (Branch) // nested switch
                  {
                    case "CSE":
                    case "CCE":
                        System.out.println("elective courses : Machine Learning, Big Data");
                    break;
                    case "ECE":
                        System.out.println("elective courses : Antenna Engineering");
                    break;
                    default:
                       System.out.println("Elective courses : Optimization");
                  }
            }
      }
 }
This was all about Java Switch Case Tutorial. Hope you like our explanation on Switch Statement in Java.
Java Quiz

4. Conclusion

In this tutorial, we studied the switch statement in Java, how to use it with different Java Switch case example. Furthermore, if you have any query feel free to ask in a comment section.

Related Topic- Java Exception Handling 

For reference 

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.