Java Switch

In Java, switch is a decision-making statement that allows you to execute different code blocks based on different conditions. In this blog, we will explore switch statements, their syntax, how to use them in Java, and provide example code snippets to illustrate each concept.

Syntax of Java Switch Statement:

switch(expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    ...
    default:
        // code block
}

How Java Switch Statement Works:

The expression in the switch statement is evaluated, and then each case statement is compared with the evaluated expression. If a match is found, the corresponding code block is executed, and the switch statement exits. If no match is found, the default code block is executed.

Example:

int day = 3;
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");
    break;
}

Output: Wednesday

In the above example, we have a switch statement that evaluates the value of the variable day. Since the value of day is 3, the case statement with the value 3 matches, and the code block for that case is executed, which prints “Wednesday” to the console.

Breaking and Default Statements:

The break statement is used to exit the switch statement once a matching case is found. If the break statement is not used, the switch statement will continue to execute the code blocks for subsequent case statements until a break statement is encountered.

The default statement is used when none of the case statements match the evaluated expression. The default statement is optional, but it’s good practice to include it in your switch statement to handle unexpected cases.

Example:

char grade = 'A';
switch (grade) {
  case 'A':
    System.out.println("Excellent");
    break;
  case 'B':
  case 'C':
    System.out.println("Good");
    break;
  case 'D':
    System.out.println("Pass");
    break;
  case 'F':
    System.out.println("Fail");
    break;
  default:
    System.out.println("Invalid grade");
    break;
}

Output: Excellent

In the above example, we have a switch statement that evaluates the value of the variable grade. Since the value of grade is ‘A’, the case statement with the value ‘A’ matches, and the code block for that case is executed, which prints “Excellent” to the console.

Nested Switch Statements:

You can also use nested switch statements in Java, which means a switch statement can be inside another switch statement.

Example:

int i = 1;
int j = 2;
switch (i) {
  case 1:
    switch (j) {
      case 1:
        System.out.println("i is 1 and j is 1");
        break;
      case 2:
        System.out.println("i is 1 and j is 2");
        break;
      default:
        System.out.println("Invalid j value");
        break;
    }
    break;
  case 2:
    System.out.println("i is 2");
    break;
  default:
    System.out.println("Invalid i value");
    break;
}

Output: i is 1 and j is 2

In the above example, we have a switch statement nested inside another switch statement. The outer switch statement evaluates the value of the variable i, and the inner switch statement evaluates the value of the variable j. Since the values of i and j are 1 and 2 respectively, the case statement with the values 1 and 2 match, and the corresponding code block is executed, which prints “i is 1 and j is 2” to the console.

Conclusion:

Switch statements in Java are a useful decision-making tool that allows you to execute different code blocks based on different conditions. They’re easy to use and understand, and they can help you write more efficient and concise code. By understanding the syntax and how to use switch statements in Java, you can become a more effective and efficient programmer.