Java Switch

The switch statement in Java is used to execute one of many blocks of code based on a provided value, known as the switch expression.

It provides an alternative to using multiple if-else statements, especially when a program needs to check for multiple cases or values.

Syntax

The syntax for a Java switch statement is as follows:

switch (expression) {
    case value1:
        // code block to be executed if expression matches value1
        break;
    case value2:
        // code block to be executed if expression matches value2
        break;
        ...
    default:
        // code block to be executed if no case matches the expression
}
  • The expression in the switch statement can be of any primitive data type: byte, short, char, int, and String (since Java 7).
  • The case statement provides the possible values that the expression can take.
  • Each case is followed by a colon : and the code block that should be executed if the expression matches the case value.
  • The break statement is used to exit the switch statement after the code block for a matching case has been executed.
  • The default case is optional and is executed if no case matches the expression.

Example

Here's an example of how to use the switch statement:

public class Main {
    public static void main(String[] args) {
        int day = 4;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}

Output: Thursday

Multiple Cases for the Same Code Block

In some cases, multiple case statements may have the same code block.

To avoid duplicating code, you can group these cases together as follows:

public class Main {
    public static void main(String[] args) {
        int score = 75;
        switch (score) {
            case 90:
            case 80:
            case 70:
            case 60:
                System.out.println("Pass");
                break;
            case 50:
            case 40:
                System.out.println("Fail");
                break;
            default:
                System.out.println("Invalid score");
        }
    }
}

Output: Pass

Switch Expression with String

Since Java 7, it is possible to use a String value as the switch expression.

Here's an example:

public class Main {
    public static void main(String[] args) {
        String day = "Thursday";
        switch (day) {
            case "Monday":
                System.out.println("Today is Monday");
        break;
        case "Tuesday":
        System.out.println("Today is Tuesday");
        break;
        case "Wednesday":
        System.out.println("Today is Wednesday");
        break;
        case "Thursday":
        System.out.println("Today is Thursday");
        break;
        case "Friday":
        System.out.println("Today is Friday");
        break;
        case "Saturday":
        System.out.println("Today is Saturday");
        break;
        case "Sunday":
        System.out.println("Today is Sunday");
        break;
        default:
        System.out.println("Invalid day");
    }
}
}

Output: `Today is Thursday`

Fall-Through in Switch Statement

A fall-through in a switch statement occurs when the code execution flows from one case to the next without a break statement.

This can be useful in some cases, but it's generally considered a bad practice as it can lead to unexpected behavior.

Here's an example of a fall-through in a switch statement:

public class Main {
    public static void main(String[] args) {
        int score = 75;
        switch (score) {
            case 90:
                System.out.println("Excellent");
            case 80:
                System.out.println("Good");
            case 70:
                System.out.println("Average");
            case 60:
                System.out.println("Pass");
                break;
            case 50:
                System.out.println("Fail");
                break;
            default:
                System.out.println("Invalid score");
        }
    }
}

Output:

Good
Average
Pass

As you can see, since there is no break statement after the case 90:, the code execution flows to the next case case 80:, and so on.

In conclusion, the switch statement in Java provides a clean and efficient way to control the flow of a program based on multiple cases.

Just be mindful of fall-through situations and ensure that the code behaves as expected.

Java Basics