example of a switch statement in java used to excute code of multiple condition

Example 1: multiple case switch java

switch (variable)
{
    case 5:
    case 6:
    etc.
    case 100:
        doSomething();
    break;
}

Example 2: Java switch with multiple cases

// Java switch with multiple cases
public class SwitchMultipleCase
{
   public static void main(String[] args)
   {
      System.out.println(multipleCase("one"));
      System.out.println(multipleCase("two"));
   }
   static String multipleCase(final String number)
   {
      String str = null;
      switch(number)
      {
         case("one"):
         case("three"):
         case("five"):
             str = "odd numbers";
         break;

         case("two"):
         case("four"):
         case("six"):
             str = "even numbers";
         break;
         default:
             str = "please enter valid input";
      }
      return str;
   }
}

Tags:

Java Example