Java Switch Statement

Java’s switch statement is a multi-way branch statement that allows you to choose from among a set of alternatives. It allows you to execute a block of code corresponding to the first matching label or case. The switch statement is useful when you have a large number of possible choices and you want to use a more efficient control structure than a series of nested if statements. The switch statement is also more readable and easier to maintain than a series of nested if statements. The switch statement uses the break statement to exit the block of code associated with each case, which helps to prevent fall-through behavior. It’s important to use the break statement at the end of each case block to indicate the end of the block and to prevent the code from falling through to the next case. If you omit the break statement, the code will continue to execute into the next case, even if a match has been found.

Java Switch

The switch statement in Java allows you to choose from among a set of alternatives. It’s a multi-way branch statement that allows you to execute a block of code corresponding to the first matching label or case.

Here is the syntax of a switch statement in Java:

switch (expression) {
  case value1:
    // code block to be executed if expression matches value1
    break;
  case value2:
    // code block to be executed if expression matches value2
    break;
  ...
  default:
    // code block to be executed if no matching case is found
}

The expression is evaluated once and compared with the value in each case. If a match is found, the code block associated with that case is executed. If no match is found, the code block associated with the default case is executed, if one is present.

It’s important to use the break statement at the end of each case block, to indicate the end of the block and to prevent the code from falling through to the next case. If you omit the break statement, the code will continue to execute into the next case, even if a match has been found. This is known as “fall-through” behavior.

Here is an example of a switch statement in Java:

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");
}

In this example, the expression is day, which has the value 3. The switch statement will find the first case that matches this value and execute the corresponding code block. In this case, the code block associated with case 3 will be executed, which prints “Wednesday”. The break statement at the end of the block indicates the end of the block and prevents the code from falling through to the next case.

Java switch case multiple values

You can use the switch statement to handle multiple cases with the same code block by using the case label followed by multiple values, separated by commas. For example:

switch (day) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    System.out.println("Weekday");
    break;
  case 6:
  case 7:
    System.out.println("Weekend");
    break;
  default:
    System.out.println("Invalid day");
}

In this example, the code block associated with the case label 1, 2, 3, 4, and 5 will all execute the same code, which prints “Weekday”.

Java switch case Examples

Example 1:

String fruit = "apple";

switch (fruit) {
  case "apple":
    System.out.println("An apple a day keeps the doctor away.");
    break;
  case "banana":
    System.out.println("Bananas are rich in potassium.");
    break;
  case "orange":
    System.out.println("Oranges are a good source of vitamin C.");
    break;
  default:
    System.out.println("I don't know that fruit.");
}

In this example, the expression is a string variable called fruit, which has the value “apple”. The switch statement will find the first case that matches this value and execute the corresponding code block. In this case, the code block associated with case "apple" will be executed, which prints “An apple a day keeps the doctor away.”

Example 2:

You can also use a switch statement to control the flow of a loop. Here is an example:

int i = 1;

switch (i) {
  case 1:
    while (i <= 10) {
      System.out.println(i);
      i++;
    }
    break;
  case 2:
    do {
      System.out.println(i);
      i++;
    } while (i <= 10);
    break;
  case 3:
    for (i = 1; i <= 10; i++) {
      System.out.println(i);
    }
    break;
  default:
    System.out.println("Invalid loop type");
}

In this example, the switch statement is used to control which type of loop to execute based on the value of i. If i is 1, a while loop is executed. If i is 2, a do-while loop is executed. If i is 3, a for loop is executed. If none of these conditions are met, the code block associated with the default case is executed.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *