WikiGalaxy

Personalize

Java Switch Statement

Introduction:

The switch statement in Java is a control flow statement that allows a variable to be tested for equality against a list of values, known as cases. It's an efficient alternative to using multiple if-else statements.


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

Example 1: Simple Switch

This example demonstrates a basic switch statement to determine the day of the week based on a number input.


int day = 3;
String dayName;
switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    default:
        dayName = "Invalid day";
}
System.out.println(dayName);
    

Console Output:

Wednesday

Switch with Fall-through

Explanation:

In Java, if you don't use a break statement, the switch cases will fall through, executing subsequent cases until a break is encountered.


char grade = 'B';
switch (grade) {
    case 'A':
        System.out.println("Excellent");
    case 'B':
        System.out.println("Good");
    case 'C':
        System.out.println("Fair");
        break;
    default:
        System.out.println("Invalid grade");
}
    

Console Output:

Good Fair

Switch with Strings

Explanation:

Java switch statements can also handle string values, allowing for more readable code, especially when dealing with specific strings.


String fruit = "Apple";
switch (fruit) {
    case "Apple":
        System.out.println("Apple is red");
        break;
    case "Banana":
        System.out.println("Banana is yellow");
        break;
    default:
        System.out.println("Unknown fruit");
}
    

Console Output:

Apple is red

Switch with Enums

Explanation:

Enums are a special Java type used to define collections of constants. Switch statements can be used with enums for better code readability and type safety.


enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
Day today = Day.WEDNESDAY;
switch (today) {
    case MONDAY:
        System.out.println("Start of the work week");
        break;
    case WEDNESDAY:
        System.out.println("Midweek");
        break;
    case FRIDAY:
        System.out.println("End of the work week");
        break;
    default:
        System.out.println("Weekend");
}
    

Console Output:

Midweek

Nested Switch Statements

Explanation:

Switch statements can be nested within each other to handle more complex conditions. This allows for decision-making based on multiple variables.


int outer = 1;
int inner = 2;
switch (outer) {
    case 1:
        switch (inner) {
            case 1:
                System.out.println("Outer 1, Inner 1");
                break;
            case 2:
                System.out.println("Outer 1, Inner 2");
                break;
        }
        break;
    default:
        System.out.println("Default case");
}
    

Console Output:

Outer 1, Inner 2

Switch with Multiple Cases

Explanation:

Multiple cases in a switch statement can execute the same block of code by omitting the break statement between them. This is useful for grouping similar cases together.


int month = 4;
String season;
switch (month) {
    case 12:
    case 1:
    case 2:
        season = "Winter";
        break;
    case 3:
    case 4:
    case 5:
        season = "Spring";
        break;
    default:
        season = "Unknown season";
}
System.out.println(season);
    

Console Output:

Spring

Switch without Break

Explanation:

If a break statement is not used, the switch statement will continue executing the next case even if it doesn't match. This is known as fall-through behavior.


int number = 2;
switch (number) {
    case 1:
        System.out.println("One");
    case 2:
        System.out.println("Two");
    case 3:
        System.out.println("Three");
        break;
    default:
        System.out.println("Unknown number");
}
    

Console Output:

Two Three

Switch with Default Case

Explanation:

The default case in a switch statement is executed if none of the other cases match the switch expression. It's similar to the else block in if-else statements.


String color = "Purple";
switch (color) {
    case "Red":
        System.out.println("Color is Red");
        break;
    case "Blue":
        System.out.println("Color is Blue");
        break;
    default:
        System.out.println("Color not recognized");
}
    

Console Output:

Color not recognized

Switch with Complex Logic

Explanation:

Switch statements can be used to perform more complex logic by combining multiple cases and using additional code within each case.


int score = 85;
switch (score / 10) {
    case 10:
    case 9:
        System.out.println("Grade: A");
        break;
    case 8:
        System.out.println("Grade: B");
        break;
    case 7:
        System.out.println("Grade: C");
        break;
    default:
        System.out.println("Grade: D");
}
    

Console Output:

Grade: B

Switch with Nested Loops

Explanation:

Switch statements can be combined with loops to perform repetitive tasks based on different conditions.


for (int i = 0; i < 3; i++) {
    int number = i;
    switch (number) {
        case 0:
            System.out.println("Zero");
            break;
        case 1:
            System.out.println("One");
            break;
        default:
            System.out.println("Two or more");
    }
}
    

Console Output:

Zero One Two or more

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025