Previous Next

Java Switch

The switch statement is employed in order to execute various actions under different conditions.

Syntax:

switch (expression) {
  case value1:
    // block of code
    break;
  case value2:
    // block of code
    break;
  default:
    // block of code
}

Example:

public class Main {
  public static void main(String[] args) {
    int wish = 1;
    switch (wish) {
      case 1:
        System.out.println("Good Morning");
        break;
      case 2:
        System.out.println("Good Day");
        break;
      case 3: 
        System.out.println("Good Evening");
        break;
      case4:
        System.out.println("Good Night");
        break;
    }
  }
}

Output:

Good Morning
Previous Next