C1CTech

Java switch Statement

In this article, you will learn to use switch statement to control the flow of your program’s execution.

Java switch Statement

When we want to execute a particular block of code/statements among many blocks we can use switch statement.

Syntax

// switch statement 
switch(expression/variable)
{
    // case statements

    // values must be of same type of expression
    case value1 :
        // Statements
        break; // break is optional

    case value2 :
        // Statements
        break; // break is optional

    // We can have any number of case statements
    // below is default statement, used when none of the cases is true. 
    // No break is needed in the default case.
    default :
        // Statements
}

This is how it works:

Flowchart:

switch-exp-img

Example

public class Example {

    public static void main(String[] args) {

        int day = 5;

      // switch statement with int data type
        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");
        }
        System.out.println("Outside switch block");

    }
}

Output

Friday
Outside switch block

Example explained

In the above example, when day = 5:

when day = 8:

Omitting the break statement

As break statement is optional. If we omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them.

Example

public class Example {

    public static void main(String[] args) {

        int day = 2;

        switch (day) {
            case 1:
                System.out.println("Monday");

            case 2:
                System.out.println("Tuesday");

            case 3:
                System.out.println("Wednesday");

            case 4:
                System.out.println("Thursday");

            case 5:
                System.out.println("Friday");

            case 6:
                System.out.println("Saturday");
                break;

            case 7:
                System.out.println("Sunday");

            default:
                System.out.println("Invalid day");
        }
        System.out.println("Outside switch block");

    }
}

Output

Tuesday
Wednesday
Thursday
Friday
Saturday
Outside switch block

Example explained

In the above example, when day = 5:

Rules for switch statement :

Exit mobile version