🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

switch case Statements

Java Switch Statement Example

The switch statement in Java is used for multi-way branching, allowing you to execute different code blocks based on the value of an expression. It provides a concise way to write code for multiple possible cases. Each case represents a different value, and the code inside the corresponding case block is executed when the value matches the expression.

Example:

public class SwitchStatementExample {
    public static void main(String[] args) {
        int dayOfWeek = 3;

        switch (dayOfWeek) {
            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 of the week");
        }
    }
}

In this example:

  • The switch statement is based on the value of dayOfWeek.
  • Each case represents a different day of the week.
  • The code inside the corresponding case block is executed when the value matches.
  • The break statement is used to exit the switch block after a case is matched.
  • If none of the cases match, the code inside the default block is executed.

It's important to note that switch expressions can be of integral types (byte, short, int, char) or an enumeration type. Starting from Java 12, switch expressions can also be used with String expressions.

public class SwitchStringExample {
    public static void main(String[] args) {
        String dayOfWeek = "Wednesday";

        switch (dayOfWeek) {
            case "Monday":
                System.out.println("Start of the week");
                break;
            case "Wednesday":
                System.out.println("Midweek");
                break;
            case "Friday":
                System.out.println("End of the week");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}

Using switch can make the code more readable and concise when dealing with multiple cases compared to a series of nested if...else statements.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.