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:

The Java code for switch example:

public class decison_making {

    public static void main(String []args) {

 

        byte numDay;

 

        numDay=7;

 

        switch (numDay){

 

        case 1 :{

 

           System.out.println("Its Monday");

 

           break;

 

        }

 

        case 2 :{

 

           System.out.println("Its Tuesday");

 

           break;

 

        }

 

        case 3 :{

 

           System.out.println("Its Wednesday!");

 

           break;

 

        }    

 

        case 4 :{

 

           System.out.println("Its Thursday");

 

           break;

 

        }    

 

        case 5 :{

 

           System.out.println("Its Friday");

 

           break;

 

        }      

        case 6 :{

 

            System.out.println("Its Saturday!");

 

            break;

 

         }

 

        case 7 :{

 

            System.out.println("Hurray, Its Sunday!");

 

            break;

         }        

        default :{

                System.out.println("Wrong entry!");

        }

        }

 

    }

}

Output:

switch statement of Java

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.

The Java code without using the break statement:

public class decison_making {

    public static void main(String []args) {

 

        byte numDay;

 

        numDay=7;

 

        switch (numDay){

 

        case 1 :{

 

           System.out.println("Its Monday");

 

        }

 

        case 2 :{

 

           System.out.println("Its Tuesday");

 

        }

 

        case 3 :{

 

           System.out.println("Its Wednesday!");

 

        }    

 

        case 4 :{

 

           System.out.println("Its Thursday");

 

        }    

 

        case 5 :{

 

           System.out.println("Its Friday");

 

        }      

        case 6 :{

 

            System.out.println("Its Saturday!");

         }

 

        case 7 :{

 

            System.out.println("Hurray, Its Sunday!");

 

         }        

        default :{

                System.out.println("Wrong entry!");

        }

        }

 

    }

}

Output:

switch break

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.

Java code:

public class decison_making {

    public static void main(String []args) {

 

        String ColorName;

 

        ColorName="red";

 

        switch (ColorName){

 

        case "black" :{

 

           System.out.println("The color is BLACK!");

           break;       

        }

 

        case "red" :{

 

           System.out.println("The color is RED!");

           break;

 

        }

 

        case "blue" :{

 

           System.out.println("The color is BLUE!");

           break;

 

        }    

 

        case "green" :{

 

           System.out.println("The color is GREEN!");

           break;

 

        }    

 

        case "yellow" :{

 

           System.out.println("The color is Yellow!");

           break;

 

        }      

 

        default :{

                System.out.println("Specify a color name!");

        }

        }

 

    }

}

Output:

switch string

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:

The code of Java switch with for loop:

public class decison_making {

    public static void main(String []args) {

 

                // Loop through 0, 1, and 2.

                for (int x = 5; x <= 25; x=x+5) {

                    // Switch on number.

                    switch (x) {

                                case 5: {

                                    System.out.println("The case 5 is true");

                                    break;

                                }

                                case 10: {

                                    System.out.println("The case 10 is true");

                                    break;

                                }

                                case 15: {

                                    System.out.println("The case 15 is true");

                                    break;

                                }

                                case 20: {

                                    System.out.println("The case 20 is true");

                                    break;

                                }

                                case 25: {

                                    System.out.println("The case 25 is true");

                                    break;

                                }   

                                default: {

                                                System.out.println("The loop is finished!");

                                }

                    }

                }

 

    }

}

Output:

switch 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.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!