Switch case statement in Java

Switch case:

Switch-case statements basically have different cases inside it and whenever the case matches with the value/expression specified then it will start executing that case.

Syntax:

switch (expression / value) {
case 1:
  // code
  break;
case 2:
  // code
  break;
case 3:
  // code
  break;
default:
  //code
}

Points to Note:

  1. The default case will execute when none of the cases executes
  2. The break statement will break the flow from the current case and program control will go to the next lines that follow the switch-case statement.

Without break:

When a break statement is missing from a case block and that case matches, it would execute every case after that case including that current case.

Let’s see how the switch-case statement works without a break keyword.

Code:

Java Code

public class TUF {
  public static void main(String[] args) {

    //Switch case
    int n = 3;
    switch (n) {
    case 1:
      System.out.println(1);
      //break;
    case 2:
      System.out.println(2);
      //break;
    case 3:
      System.out.println(3);
      //break
    case 4:
      System.out.println(4);
      //break
    default:
      System.out.println("Not matched!");
    }
  }
}

Output:

3
4
Not matched!

Explanation:

When 3 is passed to the switch case,

  • 3 is checked with case 1 (1!=3 -> false)
  • 3 is checked with case 2 (2!=3 -> false)
  • 3 is checked with case 3 (3==3 -> true), Since th case is true it will execute the statements inside it and as there is no break after case 3, case 4 and the default case will also be executed.

Without default case:

When none of the cases executes, this default case will get executed

Code:

Java Code

public class TUF {
  public static void main(String[] args) {

    //Switch case
    int n = 10;
    switch (n) {
    case 1:
      System.out.println(1);
      break;
    case 2:
      System.out.println(2);
      break;
    case 3:
      System.out.println(3);

      break;
    case 4:
      System.out.println(4);
      break;
    default:
      System.out.println("Not matched!");

    }
  }
}

Output:

Not matched!

Explanation: As case 1, case 2, case 3, and case 4 didn’t execute the default case is executed.

Note: If there is no default case and none of the cases are executed then the output will be blank or No output

Program to check timetable using switch case.

Code:

Java Code

public class TUF {
    public static void main(String[] args) {

        //Switch case
        String day = "Thursday";
        String timeTable = "";
        switch (day){
            case "Monday":
                timeTable = "Maths, Computer networks";
                break;
            case "Tuesday":
                timeTable = "Data structures , Operating systems";
                break;
            case "Wednesday":
                timeTable = "DBMS , Maths";
                break;
            case "Thursday":
                timeTable = "CP ,Soft skills";
                break;
            case "Friday":
                timeTable = "Computer networks ,Python";
                break;
            case "Saturday":
                timeTable = "Interview prep";
                break;
            default:
                timeTable ="Enter correct day to check time table";
        }
        System.out.println(timeTable);
    }
}

Output:

CP, Soft skills

Explanation:

The provided day is Thursday, so the first three days will not be executed and the Thursday case gets executed and control will go outside the switch-case statement because of the break statement.

Special thanks to Sai bargav Nellepalli for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article