Java Switch Statement

In this tutorial:

The switch statement
A switch statement with default clause
Behavior of switch without break in a case clause
Using String in switch statement

The switch statement in Java

In Java language, switch statements serve as a means to manage complex conditional operations. At times, nested if...else statements can become complex, while the switch...case statement provides a more straightforward and cleaner syntax for implementing the same logic..

Syntax:

switch (expression) {
  case value1:
    statements
  case value2:
    statements
  // …
  case valueN:
    statements
  default:
    statements
}
  • The switch statement is used when you have to select one choice from multiple choices on the basis of result of expression.
  • The expression is evaluated.
  • It looks for the first case clause whose value matches against expression value.
  • The control passes to that case clause.
  • The execution starts from the first statement after the matching case clause.
  • The execution continues until either the break statement is encountered or execution reaches the end of the switch statement.
  • If a break statement is encountered, subsequent case clause values will not be evaluated and control is transferred to the end of the switch statement.
  • The switch statement can include any number of case clauses.
  • If no case clause matches the expression, control transfers to the default statement and statements following that clause are executed. In case, there is no default clause, control is transferred to the end of the switch statement.
  • The default clause is optional.
  • The break statement is optional.

Let’s try the following example.

int month = 3;

switch (month) {
  case 1:
    System.out.println("January");
    break;
  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");
}

In the above example,

  • The expression is evaluated to value 3.
  • It is compared to values of case clauses.
  • A matching case is found, control is transferred to that case clause and its statements are executed.
  • The break statement is encountered, control is transferred to the end of the switch statement.

A switch statement with default clause

A switch statement may also have a default clause. If the expression does not match to any case clause value, the default clause is executed.

int month = 7;

switch (month) {
  case 1:
    System.out.println("January");
    break;
  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;
  default:
   System.out.println("Unknown Month");
}

In the above example, no case clause value matches the expression. The control is transferred to the default clause and statements in the default clause are executed.

Behavior of switch without break in a case clause

Let’s try another example, and see how the program executes in the absence of a break statement in a case clause.

int month = 3;

switch (month) {
  case 1:
    System.out.println("January");
    break;
  case 2:
     System.out.println("February");
    break;
  case 3:
    System.out.println("March");
  
  case 4:
    System.out.println("April");
  
  case 5:
   System.out.println("May");
 
  case 6:
   System.out.println("June");
 
  default:
   System.out.println("Unknown Month");
}

//Output: 
March
April
May
June
Unknown Month

Here is the sequence of code flow.

  • Value 3 is assigned to variable month.
  • Expression is evaluated to value 3.
  • Matching case clause found and control is transferred to that case.
  • Statements following that matching case clause are executed.
  • Following statements in that case clause and subsequent case clauses will keep executing until either found any break statement or reach at the end of switch statement.
  • Note, in the above scenario where the break statement is absent, default clause is also executed along with case clause(s). So you need to be very careful in program flow control otherwise you may experience unexpected results.

Using String in switch statement

You can also use String Object with switch statement expression.

String weekday = "wednesday";

switch (weekday) {
  case "monday":
    System.out.println("1");
    break;
  case "tuesday":
     System.out.println("2");
    break;
  case "wednesday":
    System.out.println("3");
  break;
  case "thursday":
    System.out.println("4");
  break;
  case "friday":
   System.out.println("5");
 break;
  case "saturday":
   System.out.println("6");
   break;
  case "sunday":
   System.out.println("7");
 break;
  default:
   System.out.println("0");
}

// Output: 3

In the above code value of string weekday is compared with value of expression associated with each case. In this case, 3 is printed. Please, note down, here comparison is case sensitive. i.e., "Monday" is different from "monday" and so on.