Switch Statement Java

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

Switch statement java

What a switch statement does is, it tests one variable and depending on the output it gives you multiple choices.

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
}

Flow diagram

switch statement java

 

Before we learn switch statement let me show you what we had to do with conventional way(using if statement),

Example :

public class SwitchCase
{
   public static void main(String[] args)
   {
      int adultAge;

      adultAge = 18;
 
      if(adultAge == 16)
      {
         System.out.println("statements go here for adult age 16.....");
      }
      if(adultAge == 17)
      {
         System.out.println("statements go here for adult age 17....");
      }
      ............... so on
   }
}

As you can see, above example tells you adult age. So, we have a variable called adultAge and the adult age is 18.

All you have to do right now is to make an if statement for each adult age, if adultAge is equal to 16 and then we would have to put something like “system” then you put “out” stuff like that and it will just pains in writing code.

Also Read – Best Way To Remove Single Character From String

And after 16, if adultAge is equal to 17 and then do your stuff down. Here, as you can see we do not have to type all that stuff for each number.

Any programmer would wish there was an easiest way to test adultAge for a bunch of different things.

Well actually we do not have to write ‘n’ number of if statements rather only write something called a switch statement once and it will give you many different options so instead of writing ‘n’ number of if statements let me show you guys an example on switch case statement in java,

Example : 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

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

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


How to use switch statement in java without using break statement?

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 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)


conclusion

That’s it guys. This was all about java switch case example. I hope you guys have understood the concept of how to use switch statement in java.

You can subscribe to my blog flower brackets if you haven’t already. Do share this post if you like.

Related Posts