Java switch Statement

In this chapter you will learn:

  1. What is the Java Switch Statement
  2. Syntax for Java switch Statement
  3. Example - Java Switch Statement
  4. Example - How to use the optional break statement
  5. Example - How to write nested switch Statements

Description

The switch statement is a multiway branch statement. It provides a better alternative than a large series of if-else-if statements.

Syntax

Here is the general form of a switch statement:


switch (expression) { 
case value1: /*from   w  w w  .  j ava2  s.co m*/
    statement sequence 
    break; 
case value2: 
    statement sequence 
    break; 
. 
. 
. 
case valueN: 
    statement sequence 
    break; 
default: 
    default statement sequence 
}

The value1 to valueN are the possible case values for expression. Duplicate case values are not allowed.

A break statement jumps out of switch statement to the first line that follows the entire switch statement.

Example

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://from   w ww.ja va  2 s.  c o  m
          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:

Example 2

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:/*ww w  .j  a  v a2  s.  c  om*/
        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:

Example 3

Java supports the 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: //from   w  w  w.  j  av  a  2s  .  com
          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:

Next chapter...

What you will learn in the next chapter:

  1. What is Java for loop
  2. How to write for loop
  3. Example - A simple for loop
  4. Example - loops reversively
  5. Example - Calculate the prime number with for loop
  6. Example - How to use comma in the for loop statement
  7. How to write different forms of for loop
  8. How to write nested for loops
Home »
  Java Tutorial »
    Java Langauge »
      Java Statement
Java if Statement
Java if else Statement
Java if else ladder statement
Java nested if statement
Java switch Statement
Java for loop
Java for each loop
Java while Loop
Java do while loop
Java break statement
Java continue statement
Java Comments
Java documentation comment(Javadoc)