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:

  • The switch statement first evaluates its expression and compares it with multiple cases.
  • If there is a match, execute all statements of the matching case label.
  • For example, the variable/expression is equal to value2. In this case, all statements of case label value2 gets executed.

  • The break statement is optional. If used, move the control out of the switch block. If omitted, all statements after the matching case label are executed in sequence until the end of switch statement.

  • The default statement is optional. If no match is found, then the default statement is executed, and then the control goes out of the switch block.

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:

  • switch will execute the case whose case-label is 5 and print “Friday”.
  • When it encounters break statement, the control will skip all the rest cases and move out of the switch block.
  • print “Outside switch block”.

when day = 8:

  • switch found no match case for day variable so it will execute the default case and print “Invalid day”.
  • Now the control will move out of the switch statement and print “Outside switch block”.

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:

  • switch expression gets matched with case2 and will print “Tuesday”.
  • switch found no break statement inside case2. So it goes on executing rest cases one by one.
  • Inside case6 when control found break statement, it stop executing rest of the cases and moves out of the switch block.
  • print “Outside switch block”.

Rules for switch statement :

  • Duplicate case label values are not allowed.
  • Case labels must end with a colon ( : )
  • The value for a case must be of the same data type as the variable in the switch.
  • The value for a case must be Constant. Variables are not allowed.
  • The break statement is optional. If omitted, execution will continue on into the next case.
  • The default statement is optional and can appear anywhere inside the switch block. In case, if it is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.

Leave a Reply