switch statement

The switch statement is a multiway branch statement.

It provides a better alternative than a large series of if-else-if statements.
Here is the general form of a switch statement:

switch (expression) { 
case value1: 
    statement sequence 
    break; 
case value2: 
    statement sequence 
    break; 
. 
. 
. 
case valueN: 
    statement sequence 
    break; 
default: 
    default statement sequence 
}

Duplicate case values are not allowed.

A break statement jumps out of switch statement to the first line that follows the entire switch statement.
Here is a simple example that uses a switch statement:
  
public class Main {
  public static void main(String args[]) {
    for (int i = 0; i < 6; i++)
      switch (i) {
        case 0:
          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.");
      }
  }
}  

The output produced by this program is shown here:


i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.

The break statement is optional

If you omit the break, execution will continue on into the next case. For example, consider the following program:

  
public class Main {
  public static void main(String args[]) {
    for (int i = 0; i < 12; i++)
      switch (i) {
        case 0:
        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");
      }
  }
}  

This program generates the following output:


i is less than 5 
i is less than 5 
i is less than 5 
i is less than 5 
i is less than 5 
i is less than 10 
i is less than 10 
i is less than 10 
i is less than 10 
i is less than 10 
i is 10 or more 
i is 10 or more

Nested switch Statements

For example, the following fragment is a valid nested switch statement.

  
public class Main {
  public static void main(String args[]) {
    for (int i = 0; i < 6; i++)
      switch(i) { 
        case 0: 
          switch(i+1) { // nested switch 
            case 0: 
              System.out.println("target is zero"); 
              break; 
            case 1: 
              System.out.println("target is one"); 
              break; 
          } 
          break; 
        case 2: // ...
     }
  }
}
 

The output:


target is one
Home 
  Java Book 
    Language Basics  

Statement:
  1. Simplest if statement
  2. If else statement
  3. switch statement
  4. while loop
  5. do-while statement
  6. for Loop
  7. for each loop
  8. break to Exit a Loop
  9. continue
  10. return statement returns from a method.
  11. Comments