Q: Java Switch Statement I have a problem using switch statement when I tried to deal with a special situation.
For example,
I have 3 cases: A, B, C.


*

*for A, I want to do statement_1 and statement_3.

*for B, I want to do statement_2 and statement_3.

*for C, I want to do nothing


if I use if-else statement, it will look like the following:
 if ( not C){
    do statement_3

   if B
      do statement 2
   else if A
      do statement 1

 }

If I want to use switch statement to do the same thing, I have some trouble.
switch (variable){
case A:  do statement_1
case B: do statement_2
// how to do statement 3 here?  
}

I am trying to avoid the duplicated codes. So I am thinking that how to make the codes as simple as I can.
UPDATE 1:


*

*to make my codes/question more clear, I just want to make my codes
as simple/clear as I can, that is why I want to use switch statement
instead of if-else. Also, I heard that switch-statement is usually
faster than if-else. (I am not 100% sure though).

*I want to use switch-case because Case A, B, C are enum type. they
are not variable. Sorry about the confusion.

*each statements are more than 10 lines of codes. That is why I do not want to do the followings:
switch (enum variable) {
case A:
  statement1
  statement3
 break;
case B:
  statement2
  statement3
 break;

}

A: You can do this:
switch (variable){
  case A:  do statement_1; do statement_3; break;
  case B:  do statement_2; do statement_3; break;    
}


A: i would recommend to define exactly what staments should be executed:
switch (variable){
    case A: 
        statement_1();
        statement_3();
        break;
    case B: 
        statement_2();
        statement_3();
        break;
}

for Update-3:
create methods for those 10 lines:
public void statement_1() {
    //your 10 lines of code
}

if you're always executing statement_3, except for case C you can go with if/else-blocks as you wrote them.
but in my honest opinion:
define EXACTLY what has to be done in which case if you have a small amount of cases.
it is easier to read for others

A: Why not nest the switch into the if statement? there is no-repeat code this way.
if(!C){
    statement_3;
    switch(variable){
    case A:
        statement_1;
        break;
    case B: 
        statement_2;
        break;
}

or make use of both the if-statement and the switch?
if(!C){
    statement_3;
}
switch(variable){
case A:
    statement_1;
    break;
case B: 
    statement_2;
    break;


A: I often find introducing enums adds clarity. Here I imagine each enum is an issue which can be resolved through a number of processes:
enum Issue {
  A {
    void handleIt () {
      statement_1();
      statement_3();
    }
  },
  B {
    void handleIt () {
      statement_2();
      statement_3();
    }
  },
  C {
    void handleIt () {
      // Do nothing.
    }
  },
  D {
    void handleIt () {
      A.handleIt();
      B.handleIt();
    }
  };

  abstract void handleIt();
}

Note here that you get the added benefit of being able to handle certain issues using the solutions of other issues (see my D enum).

A: If a case has more than 2-3 statements it's bette(from point of view of readability and clean code) to extract them as separate methods:
switch (variable){
    case A: handleCaseA(); break;
    case B: handleCaseB(); break;
    default: handleDefaultCase(); break;
}


A: switch (variable) {
case A:  
do statement_1;
do statement_3;
break;
case B:
do statement_2;
do statement_3;
break;
}


A: With Java 12 switch-expressions you can do it nicely as follows:
switch (variable) {
        case A -> statement_1();
                  statement_3();
        case B -> statement_2();
                  statement_3();
};


A: Here u must use if statement in this way because in switch there are normally default value also.
    if (letter == 'A' || letter == 'B') { System.out.println("Statement 3 ");}

switch (letter) {
            case 'A':
                System.out.println("Statement 1 ");
                break;

            case 'B':
                System.out.println("Statement 2 ");
                break;

            case 'C':
                System.out.println();
                break;
            default:
                System.out.println("You entered wrong value");
}       


A: When you use "case," use a switch statement first, and make sure you've got an initialized variable in it. Here's an example.
Use an initialized variable: i.e.
(String s=sc.nextLine();)

        switch (s) {
        case "Pepperoni":
            System.out.println("Pepperoni? Nice choice. Please wait............Ding! Here's your pepperoni pizza. Enjoy, and thanks for using the Local Pizzeria!");
        case "Cheese":
            System.out.println("Cheese? Okay! Soft cheese it is! Please wait............Ding! Here's your cheese pizza. Enjoy, and thank you for using the Local Pizzeria!");
            break;
        case "Mushrooms":
            System.out.println("Mushrooms? Veggie person, right? OK! Please wait............Ding! Here's your mushroom pizza. Enjoy, and thanks for using the Local Pizzeria!");
            break;
        case "Beef Bits":
            System.out.println("Beef bits? Like meat? Alright! Please wait............Ding! Here's your beef bit pizza. Enjoy, and thank you for using the Local Pizzeria!");

            break;
        case "Spicy Buffalo Chicken":
            System.out.println("Spicy buffalo chicken? Way to spice things up! Please wait............Ding! Here's your spicy buffalo chicken pizza. Enjoy, and thank you for using the Local Pizzeria!");
            break;
        case "Exit":
            System.out.println("See you soon!");
        }
    }

