Skip to main content

Java Switch Statement

This guide provides a detailed explanation of the Java switch statement with examples.

Basic Syntax

switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    ...
    default:
        // code block
}

The switch statement evaluates an expression, then executes the corresponding case.

Example 1: Days of the Week

int day = 4;
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(dayName);

Console Output

Thursday

Example 2: Grade System

char grade = 'B';
String description;

switch (grade) {
    case 'A':
        description = "Excellent";
        break;
    case 'B':
        description = "Good";
        break;
    case 'C':
        description = "Average";
        break;
    case 'D':
        description = "Below Average";
        break;
    case 'F':
        description = "Fail";
        break;
    default:
        description = "Invalid grade";
}

System.out.println("Grade " + grade + " is " + description);

Console Output

Grade B is Good

Fall-through Behavior

Without a break statement, execution will continue to the next case.

int number = 2;
String size;

switch (number) {
    case 1:
    case 2:
    case 3:
        size = "Small";
        break;
    case 4:
    case 5:
    case 6:
        size = "Medium";
        break;
    case 7:
    case 8:
    case 9:
        size = "Large";
        break;
    default:
        size = "Unknown";
}

System.out.println("Size: " + size);

Console Output

Size: Small

Default Case

The default case is executed when no other case matches.

String fruit = "Mango";
String category;

switch (fruit) {
    case "Apple":
    case "Pear":
        category = "Pome fruit";
        break;
    case "Cherry":
    case "Plum":
        category = "Stone fruit";
        break;
    case "Lemon":
    case "Lime":
        category = "Citrus fruit";
        break;
    default:
        category = "Unknown category";
}

System.out.println(fruit + " is a " + category);

Console Output

Mango is a Unknown category

Switch Expressions (Java 12+)

Java 12 introduced switch expressions, allowing switch to be used as an expression.

int month = 8;
String season = switch (month) {
    case 12, 1, 2 -> "Winter";
    case 3, 4, 5 -> "Spring";
    case 6, 7, 8 -> "Summer";
    case 9, 10, 11 -> "Autumn";
    default -> "Invalid month";
};

System.out.println("The season is " + season);

Console Output

The season is Summer

Interactive Example

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!