Switch statement in Java

Like the if statement, this statement allows us to modify the execution line. Instead of using a boolean expression, it uses an expression; byte, short, char, int, or an enumerated type. Starting from Java version 1.7, you can also use String.

Switch statement syntax

switch (<expression>) {
case <value>:
	<sequence of statements separated by semicolons>;
case <value>:
	<sequence of statements separated by semicolons>;
…
default:
	<sequence of statements separated by semicolons>;
}

 

The type of the <value> and the <expression> must match. The default clause is optional. There may be as many clauses case as we want. The <value> can not be an expression, can only be a literal.

How does the switch statement work

When the switch statement is reached, the expression is evaluated. The result is compared with each <value> consecutively, until you find one that matches. When a <value> matches, the instructions in that clause, and the instructions in all of the clauses that follow are executed.

If we want to execute the instructions of a single clause, we usually put a "break" after the last instruction in <sequence of statements separated by semicolons>, which stops the execution of the switch statement.

Syntax of the switch statement with break

switch (<expression>) {
case <value>:
	<sequence of statements separated by semicolons>;
	break;
case <value>:
	<sequence of statements separated by semicolons>;
	break;
…
default:
 	<sequence of statements separated by semicolons>;
}




Example without break; “Days of the week left”

In this example, we enter a day of the week in English and the program returns the list the days of the week we have left to finish the week.

package com.edu4java.javatutorials;

import javax.swing.JOptionPane;

public class SwitchExample {
	
public static void main(String[] args) {
	String daysLeft = "";
	String weekDay = JOptionPane.showInputDialog("Enter the day of the week");

	switch (weekDay.toUpperCase()) {
	case "SUNDAY":
		daysLeft = daysLeft + "Sunday ";
	case "MONDAY":
		daysLeft = daysLeft + "Monday ";
	case "TUESDAY":
		daysLeft = daysLeft + "Tuesday ";
	case "WEDNESDAY":
		daysLeft = daysLeft + "Wednesday ";
	case "THURSDAY":
		daysLeft = daysLeft + "Thursday ";
	case "FRIDAY":
		daysLeft = daysLeft + "Friday ";
	case "SATURDAY":
		daysLeft = daysLeft + "Saturday ";
	}
	JOptionPane.showMessageDialog(null, "We have \"" + daysLeft + "\" to end the week");
}
}

  

We do not use the optional default clause. The weekDay.toUpperCase () method converts all lowercase letters present in the String object to uppercase, so the comparison works even when we enter the day of the week in lowercase.

Example using break; "menu of options"

This is a common case where our program makes the user choose from several options:

package com.edu4java.javatutorials;

import javax.swing.JOptionPane;

public class SwithExample2 {
	public static void main(String[] args) {
		String menu = new String("Choose one option: 1, 2, 3 o 4 \n");
		for (int i = 1; i <= 4; i++) {
			menu = menu + " option " + i + "\n";
		}
		String option = JOptionPane.showInputDialog(menu.toString());
		switch (option) {
		case "1":
			JOptionPane.showMessageDialog(null,
					"Congratulations, you have chosen option 1");
			break;
		case "2":
			JOptionPane.showMessageDialog(null,
					"Congratulations, you have chosen option 2");
			break;
		case "3":
			JOptionPane.showMessageDialog(null,
					"Congratulations, you have chosen option 3");
			break;
		case "4":
			JOptionPane.showMessageDialog(null,
					"Congratulations, you have chosen option 4");
			break;
		default:
			JOptionPane.showMessageDialog(null, option
					+ " is not a valid option");
			break;
		}
	}

}

  

In this case we use break in each case clause and only one option is executed. We also use the default clause to indicate that the selected option does not correspond to any valid option.

Criticism of the use of the switch statement

The use of the switch statement in different languages ​​has been criticized for creating a source code which violates the structured programming paradigm. In OOP, this criticism is increased because of the existing object-oriented design patterns that replaces the switch function.

Design patterns are recommended practices that improve the quality of object-oriented programs.