Switch statement java

Hello everyone!! Welcome to flower brackets blog. In this post we are going to delve into switch statement java.

Switch statement allows a variable to be tested against list of values. Here each value is called as case and variable being switched is checked for each case.

Here’s the syntax for switch case java,

Syntax

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

   case 2:
   // code goes here
   break;
   
   case 3:
   // code goes here
   break;
   .
   .
   .
   .
   case N:
   // code goes here
   break;

   default:
   // default code goes here
}

switch statement java

public class SwitchCase
{
   public static void main(String[] args)
   {
      int a = 2;
      switch(a)
      {
         case 1:
             System.out.println("Case 1");
             break;

         case 2:
             System.out.println("Case 2");
             break;

         case 3:
             System.out.println("Case 3");
             break;

         case 4:
             System.out.println("Case 4");
             break;

         case 5:
             System.out.println("Case 5");
             break;

         default:
             System.out.println("Default case");
      } 
   }
}

Output :

Case 2

Switch statement is used with break statement. Here break statement is optional. Break statement is used when you want the program flow to come out of switch body.

Also read – remove character from string

By using switch case in java, execution flow will directly come out of switch statement, ignoring rest of the cases.


What happens if there is no break in a switch statement in java?

As we discussed earlier break statement is optional. Meanwhile if we do not use break statement execution will continue into next case.

Also read – learn static keyword in java

Now let’s see an example on multiple case statements without break statement,

public class JavaSwtich 
{
   public static void main(String[] args) 
   {
      int num = 5;
      String strWeekType;
      String strDay;

      switch(num) 
      {
         case 1 : strDay = "Monday";
                  break;
         case 2 : strDay = "Tuesday";
                  break;
         case 3 : strDay = "Wednesday";
                  break;
         case 4 : strDay = "Thursday";
                  break;
         case 5 : strDay = "Friday";
                  break;
         case 6 : strDay = "Saturday";
                  break;
         case 7 : strDay = "Sunday";
                  break;
         default : strDay = "Invalid day!!";
      }

      switch(num) 
      {
         // java switch multiple cases without break statements 

         case 1 : 
         case 2 :
         case 3 :
         case 4 :
         case 5 :
              strWeekType = "Weekday";
              break;
         case 6 :
         case 7 :
              strWeekType = "Weekend"; 
              break;

         default : strWeekType = "Invalid week type!!";
      } 
      System.out.println(strDay + " is a " + strWeekType);
   }
}

Output :

Friday is a Weekday


Nested switch statements in java

In java nested switch, switch statement determines its own block so there will be no conflicts between case constants in the inner switch and outer switch.

Also read – remove vowels from string java

Now let’s see an example on nested switch java,

public class SwitchSatement 
{
   public static void main(String[] args) 
   {
      String Branch = "TCE";
      int year = 2;

      switch(year)
      {
         case 1 :
            System.out.println("Elective subjects : Distributed Systems, Network Security");
            break;
         case 2 :
            switch(Branch) // nested switch java
            {
               case "TCE":
               case "ECE":
                  System.out.println("Elective subjects : Random Processes, Real Time Systems");
                  break;

               case "EEE":
                  System.out.println("Elective subjects : Electrical Engineering Materials");
                  break;

               default:
                  System.out.println("Elective subjects : Operations Research");
            }
      }
   }
}

Output :

Elective subjects : Random Processes, Real Time Systems


NOTE :

  1. In the above example we can’t use break statement after default, because control would itself come out of the switch after default.
  2. Meanwhile there’s no harm using break statement after default.
  3. Nested switch statements are allowed and should be avoided and makes the program complex.
  4. Expression inside switch should result in constant value else would be invalid.
  5. Case need not have to be in order and you can specify them in any order. Also, it need not be in ascending order.
  6. We can also use characters in switch statement in java.
  7. Valid expressions for switch statement are as below,
Valid:

switch(5 + 6 + 56)
switch(5 * 6 + 7 % 8)

Invalid:

switch(pq + rs)
switch(p + q + r)

Related Posts