Java switch case statement with 4 examples and code

The switch case statement in Java

In Java programming language, the switch is a decision-making statement that evaluates its expression.

This is how the switch statement in Java works:

  • The switch block, which is the body of switch statement may contain one or more case labeled statements.
  • It may also contain a default label.
  • After evaluating the expression, the statements in the matched case are executed.
  • You may use different data types in switch Java statement. It may work with primitive data types including int, char, byte and short.
  • The switch statement also works with the String class of Java, enumerated types, and a few special classes.
  • Each case contains a break statement. The break statement is necessary to exit the switch statement (see explanation in the first example).

See the following section for structure and examples of using the switch case statement. After that, I will explain the difference between the if and switch statement and which one to use depending on the scenario.

Structure of switch case statement in Java

The general way of using the switch case is:

switch (expression) {

case 1 :

// Java statement here if case 1 is evaluated as true

break;  // It will terminate the switch statement

case 2 :

// Java statement here if case 2 is evaluated as true

brerak;

default:  // It will execute if none of the case is true

{

// Java statement here

}

An example of using switch case and default statements

In this example, the switch statement is used with seven cases. A variable is assigned a value (number of the day in Week) which is used as an expression in the switch statement. After that, seven case statements are used; one for each day of the Week. For example, if the value of numDay variable is 1, the output will be Monday and for the value of 7, it should be Sunday. If any other value than 1 to 7 is assigned, the default case will execute:

switch statement of Java

See online demo and code

The Java code for switch example:

 

You see, I assigned the value 7 to the variable which is used as an expression in the switch statement. The case number seven evaluated as true, so its respective statement is executed.

Also, the break statement is used in each case statement. As mentioned earlier, it is necessary to use the break statement in each case, otherwise, statements in switch block will fall through.  That means, If you do not use the break statement, the execution of switch cases will go on even after matched case is found. If you have used a default case, it will also execute.

See the same example as above where I did not use the break statement in any case label.

switch break

See online demo and code

The Java code without using the break statement:

 

You can see in the output of above program, the statement inside the default case is also executed.

A demo of using string in switch case Java statement

In this example, a string variable is used as the switch expression which is evaluated in five cases. The variable is assigned a color name. Five different cases are used for black, green, red, blue and yellow. Each case label will display the respective message if evaluated as true, otherwise, default case will execute.

switch string

See online demo and code

The code:

 

An example of using for loop with switch case

You may also place the switch case statement inside a for loop. In each iteration of the for loop, the switch case can be executed for each value in the range of for loop. See this example where a for loop is imitated with a value of 5. The x variable is incremented by 5 in each iteration. In the body of for loop, the switch statement with five cases and a default label is used. For every loop, the expression in the switch is matched and the matched case statement will display a message. Have a look:

switch for loop

See online demo and code

The code of Java switch with for loop:

 

Difference between if and switch statements

In the switch statement, multiple execution paths can be given unlike in the if statement. Another difference is that the if statement can test expression that is based on ranges of conditions or values. In the case of the switch statement, the expressions are tested based on a single integer, enumerated value or the string object.

Also, you should consider readability while deciding which decision-making statement to use.