Home Java Tutorial Java switch Statement

Java switch Statement

by Anup Maurya
39 minutes read

In this tutorial, you will learn to use the switch statement in Java to control the flow of your program’s execution with the help of examples.

The switch statement is a conditional statement in Java programming that allows you to execute different blocks of code based on the value of a variable or expression. It is often used as an alternative to the if…else statement when you have multiple conditions to check.

The general syntax of a switch statement in Java is as follows:

switch (expression) {
    case value1:
        // code to be executed if expression is equal to value1
        break;
    case value2:
        // code to be executed if expression is equal to value2
        break;
    .
    .
    .
    case valueN:
        // code to be executed if expression is equal to valueN
        break;
    default:
        // code to be executed if none of the values match expression
}

Here, expression is the variable or expression whose value you want to check, and value1 through valueN are the possible values that expression can have. If expression matches one of the values, the code inside the corresponding case block will be executed. The break statement is used to exit the switch statement after a case is executed. The default block is executed if none of the values match expression.

Let’s see some examples to understand how the switch statement works in Java.

Example 1:

int day = 1;
String dayName;
switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    case 4:
        dayName = "Thursday";
        break;
    case 5:
        dayName = "Friday";
        break;
    case 6:
        dayName = "Saturday";
        break;
    case 7:
        dayName = "Sunday";
        break;
    default:
        dayName = "Invalid day";
}
System.out.println("The day is " + dayName);

In this example, we have declared a variable day and assigned it a value of 1. The switch statement checks the value of day and assigns a corresponding dayName value based on the case block that matches. Since day is equal to 1, the first case block is executed, and dayName is assigned the value “Monday”. The output of this program is “The day is Monday”.

Example 2:

char grade = 'B';
String message;
switch (grade) {
    case 'A':
        message = "Excellent";
        break;
    case 'B':
        message = "Good";
        break;
    case 'C':
        message = "Average";
        break;
    case 'D':
        message = "Poor";
        break;
    case 'F':
        message = "Fail";
        break;
    default:
        message = "Invalid grade";
}
System.out.println("Your grade is " + grade + ", which is " + message);

In this example, we have declared a variable grade and assigned it a value of ‘B’. The switch statement checks the value of grade and assigns a corresponding message value based on the case block that matches. Since grade is equal to ‘B’, the second case block is executed, and message is assigned the value “Good”. The output of this program is “Your grade is B, which is Good”.

Note that the switch statement only works with primitive data types (such as int, char, and boolean) and enumerated types in Java 7 and later. It does not work with strings or objects.

In summary, the switch statement is a powerful tool in Java that allows you to check for multiple conditions using a simple syntax. With the switch statement, you can group together all of your conditions and execute the corresponding block of code based on the value of the expression being checked.

related posts

Leave a Comment