Switch statement in java

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

Switch statement in java

Switch statement executes statements based on value of variable or expression.

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 in java

public class SwitchStatementJava
{
   public static void main(String[] args) 
   { 
      int year = 2;
      switch(year) // switch is evaluated only once
      {
         case 1:
             System.out.println("January");
             break; // break keyword is optional

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

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

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

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

         case 6:
             System.out.println("June");
             break;

         case 7:
             System.out.println("July");
             break;

         case 8:
             System.out.println("August");
             break;

         case 9:
             System.out.println("September");
             break;

         case 10:
             System.out.println("October");
             break;

         case 11:
             System.out.println("November");
             break;

         case 12:
             System.out.println("December");
             break;

         default: // default keyword is optional
             System.out.println("Default case");
      } 
   }
}

Output :

February

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

Also read – remove character from string

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


NOTE :

  1. In the above example we can’t use break statement after default, because control itself come out of switch after default.
  2. Meanwhile there’s no harm using break statement after default.
  3. Duplicate case value are not allowed. Case value should be of same data type.
  4. Expression inside switch should result in constant value else would be invalid.
  5. Case need not have to be in order you can specify them in any order. Also, it need not be in ascending order.
  6. In switch statement variable used are byte, short, integer, strings and enums.
  7. We can have any number of case statements within switch.
  8. Data type for a case must be same.
  9. We can also use characters in switch statement.
  10. 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