Java switch statement

Introduction

The Java switch statement can run to different parts of code based on the value of an expression.

It is an alternative for if-else-if statements.

Here is the general form of a switch statement:

switch (expression) {  
  case value1:  /*from   ww w  . j a va  2 s.  c o  m*/
      // statement sequence  
      break;  
  case value2:  
      // statement sequence  
      break;  
.  
.  
.  
  case valueN :  
      // statement sequence  
      break;  
  default:  
     // default statement sequence  
} 

The expression must be of type byte, short, int, char, an enumeration or type String.

Each value in the case statements must be a unique constant expression.

Duplicate case values are not allowed.

The type of each value must be compatible with the type of expression.

The default statement is optional.

The break statement can terminate a statement sequence.

// A simple example of the switch.
public class Main {
  public static void main(String args[]) {
    for(int i=0; i<6; i++)
      switch(i) {
        case 0://from   w  ww. j a  v a  2  s  .  c om
          System.out.println("i is zero.");
          break;
        case 1:
          System.out.println("i is one.");
          break;
        case 2:
          System.out.println("i is two.");
          break;
        case 3:
          System.out.println("i is three.");
          break;
        default:
          System.out.println("i is greater than 3.");
      }
  }
}

Java Swtich with optional break

The break statement is optional in switch statement.

If you omit the break, execution will continue on into the next case.

For example,

// In a switch, break statements are optional.
public class Main {
  public static void main(String args[]) {
    for(int i=0; i<12; i++)
      switch(i) {
        case 0:/* w w w  .  j av a 2  s .c  o  m*/
        case 1:
        case 2:
        case 3:
        case 4:
          System.out.println("i is less than 5");
          break;
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
          System.out.println("i is less than 10");
          break;
        default:
          System.out.println("i is 10 or more.");
      }
  }
}

The following code check season for a month via switch statement.

// An improved version of the season program.
public class Main {
  public static void main(String args[]) {
    int month = 4;
    String season;/* w ww .  j  a  v  a2  s .  c  o  m*/

    switch (month) {
      case 12: 
      case 1: 
      case 2:
        season = "Winter";
        break;
      case 3: 
      case 4: 
      case 5:
        season = "Spring";
        break;
      case 6: 
      case 7: 
      case 8:
        season = "Summer";
        break;
      case 9: 
      case 10: 
      case 11:
        season = "Autumn";
        break;
      default:
        season = "Bogus Month";
    }
    System.out.println("April is in the " + season + ".");
  }
}

Java switch statement on String

From JDK 7 we can use String to control a switch statement.

// Use a string to control a switch statement.
public class Main {
  public static void main(String args[]) {

    String str = "two";

    switch(str) {
      case "one":
        System.out.println("one");
        break;/*from www. j  ava  2 s . c o m*/
      case "two": 
        System.out.println("two");
        break;
      case "three":
        System.out.println("three");
        break;
      default: 
        System.out.println("no match");
        break;
    }
  }
}



PreviousNext

Related