13

My intention is to call two cases inside another case in the same switch statement,

switch (orderType) {
        case 1: 
            statement 1;
            break;
       case 2:
            statement 2;
            break;
       case 3:
             **call case 1;**
             **Call case 2;**
             break;
       default:
            break;`
}

Can we do that in Java?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Lasitha Konara
  • 1,111
  • 3
  • 12
  • 19
  • 1
    I don't think so, why you want to do it ? – AnkeyNigam Jul 19 '15 at 06:26
  • Using "switch-case" meant to make your code easier to understand, don't try to over complicate it. – Maroun Jul 19 '15 at 06:26
  • Relevant [Passing a value from one case to another in switch statement](http://stackoverflow.com/questions/16621708/passing-a-value-from-one-case-to-another-in-switch-statement) – Bhargav Rao Jul 19 '15 at 06:27
  • @MarounMaroun I'm puzzled. You closed the question as a duplicate of a question dealing with _nested_ switch-case-statements. But this question is about code reusage of another case in the _same_ switch. Or did I understand this wrong? – Seelenvirtuose Jul 19 '15 at 06:29
  • @Seelenvirtuose I reopened, though the answer for this question is very similar. – Maroun Jul 19 '15 at 06:31
  • @MarounMaroun I agree about the similarity of the answers, and I think that would have been a good reason for closing this one. I just thought, it's not really a duplicate ... – Seelenvirtuose Jul 19 '15 at 06:33
  • @Seelenvirtuose You're right and thanks for the comment. I didn't pay enough attention. – Maroun Jul 19 '15 at 06:33
  • Hm, there's a downvote but this question seems fine and valid to me. Not quite useful but yeah. – Saturn Jul 19 '15 at 06:35
  • I guess you can put case 3: before case 1: and case 2. In case 3: remove the break; and add orderType=1. This will cause case 1: to be executed. It is NOT a nice solution and I would try yo avoid it. – c0der Jul 19 '15 at 07:40

6 Answers6

10

No, you can't jump to the code snippet in another switch case. You can however extract the code into an own method that can be called from another case:

switch (orderType) {
    case 1: 
        someMethod1();
        break;
    case 2:
        someMethod2();
        break;
    case 3:
        someMethod1();
        someMethod2();
        break;
    default:
        break;
}

void someMethod1() { ... }
void someMethod2() { ... }
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
9

Although you cannot influence the switch cases directly, you can call switch's parent method from one case and pass different arguments. For example,

void foo(int param1, String param2, ...) {
    switch (param1) {
        case 0:
            foo(1, "some string");
            break;

        case 1:
            //do something
            break;

        default:
            break;
    }
}
sandalone
  • 41,141
  • 63
  • 222
  • 338
2

You can't just call a another case block like this. What you could do, though, is wrap each of these blocks in a method, and reuse them:

private void doSomething() {
    // implementation
}

private void doSomethingElse() {
    // implementation
}

private void runSwitch(int order) {

    switch (orderType) {
           case 1: 
                doSomething();
                break;
           case 2:
                doSomethingElse();
                break;
           case 3:
                doSomething();
                doSomethingElse();
                break;
           default:
                break;
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Using methods is the best way to do it as mentioned in the accepted answer but for some reasons if you are unable to create method, Here is one more way to do this without using methods

boolean isBreakTheLoop = false, isCase2Required = false;
while(!isBreakTheLoop){
    switch (orderType) {
       case 1: 
            statement 1;
            if(isCase2Required){
               orderType = 2;
               break;
            }
            isBreakTheLoop = true;
            break;
       case 2:
            statement 2;
            isBreakTheLoop = true;
            break;
       case 3:
            orderType = 1;
            isCase2Required = true;
            break;
       default:
            isBreakTheLoop = true;
            break;
    }
}
Pavan Kumar
  • 164
  • 15
0

Just a reminder: while the most suggested case is the best solution, note that if you don't close with a brake; your case:, it will continue executing in the next case.

Then, while it's still best pratice to break your cases, you could still adopt a solution similar to the following:

switch (orderType) {
    case 3:
        someMethod1();
    case 2:
        someMethod2();
        break;
    case 1: 
        someMethod1();
        break;
    default:
        break;
}

Note that "avoiding breaks" solution can't completely cover OP necessities, because writing:

    case 3:
    case 1:
        someMethod1();
    case 2:
        someMethod2();
    default:
        break;

or:

    case 3:
    case 1:
        someMethod1();
        break;
    case 2:
        someMethod2();
        break;
    default:
        break;

would make case: 3 be the same than case: 1, making them execute both methods in first code, or a single method in the second code.

Patcha
  • 1
  • 3
-2

You can use this trick in some case :

switch (orderType) {
   case 3: 
        statement 3;
   case 2:
        statement 2;
   case 1:
        statement 1;
        break;
   default:
        break;`

}