Can we call a "case" inside another case in the same switch statement in Java?
Asked Answered
C

6

14

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?

Cordes answered 19/7, 2015 at 6:23 Comment(9)
I don't think so, why you want to do it ?Festination
Using "switch-case" meant to make your code easier to understand, don't try to over complicate it.Willena
Relevant Passing a value from one case to another in switch statementSecede
@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?Christine
@Christine I reopened, though the answer for this question is very similar.Willena
@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 ...Christine
@Christine You're right and thanks for the comment. I didn't pay enough attention.Willena
Hm, there's a downvote but this question seems fine and valid to me. Not quite useful but yeah.Brake
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.Prognostication
T
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;
    }
}
Tullius answered 7/1, 2016 at 15:35 Comment(0)
C
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() { ... }
Christine answered 19/7, 2015 at 6:26 Comment(0)
T
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;
    }
}
Tullius answered 7/1, 2016 at 15:35 Comment(0)
F
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;
    }
}
Foreknowledge answered 19/7, 2015 at 6:27 Comment(0)
U
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;
    }
}
Usual answered 24/1, 2020 at 5:54 Comment(0)
C
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.

Cadaver answered 30/1, 2023 at 14:49 Comment(2)
This was already proposed 4 years ago, see below.Blagoveshchensk
The case from 4 years ago was very generical and like saying it could totally fullfill OP needs. My post explain it is an alternative, but limited as solution. And it explains why.Cadaver
M
-1

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;`

}

Marek answered 4/1, 2019 at 14:3 Comment(1)
Your answer does not address the questionHoleandcorner

© 2022 - 2024 — McMap. All rights reserved.